Title: How to modify Related Posts function
Last modified: August 21, 2016

---

# How to modify Related Posts function

 *  [Uli](https://wordpress.org/support/users/uli/)
 * (@uli)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/how-to-modify-related-posts-function/)
 * Fantastic theme.
    Following up on previous post about [removing Related Posts](https://wordpress.org/support/topic/how-to-modify-related-posts-function/wordpress.org/support/topic/how-to-remove-or-disable-related-posts-function?output_format=md),
   I would like to modify the Related Posts function as follows:
 * For all posts in category ID 4, I want to show in the Related Posts section posts
   from category ID 5 (yes, they are not related posts by tag or category in the
   strictest sense). My assumption is that I have to include an if/else section 
   inside the **if ( ot_get_option(‘related-posts’) == ‘categories’ )** part of 
   functions.php
 * Can anybody provide the revised code? THANKS!
 *     ```
       /*  Related posts
       /* ------------------------------------ */
       if ( ! function_exists( 'alx_related_posts' ) ) {
   
       	function alx_related_posts() {
       		wp_reset_postdata();
       		global $post;
   
       		// Define shared post arguments
       		$args = array(
       			'no_found_rows'				=> true,
       			'update_post_meta_cache'	=> false,
       			'update_post_term_cache'	=> false,
       			'ignore_sticky_posts'		=> 1,
       			'orderby'					=> 'rand',
       			'post__not_in'				=> array($post->ID),
       			'posts_per_page'			=> 3
       		);
       		// Related by categories
       		if ( ot_get_option('related-posts') == 'categories' ) {
   
       			$cats = get_post_meta($post->ID, 'related-cat', true);
   
       			if ( !$cats ) {
       				$cats = wp_get_post_categories($post->ID, array('fields'=>'ids'));
       				$args['category__in'] = $cats;
       			} else {
       				$args['cat'] = $cats;
       			}
       		}
       		// Related by tags
       		if ( ot_get_option('related-posts') == 'tags' ) {
   
       			$tags = get_post_meta($post->ID, 'related-tag', true);
   
       			if ( !$tags ) {
       				$tags = wp_get_post_tags($post->ID, array('fields'=>'ids'));
       				$args['tag__in'] = $tags;
       			} else {
       				$args['tag_slug__in'] = explode(',', $tags);
       			}
       			if ( !$tags ) { $break = true; }
       		}
   
       		$query = !isset($break)?new WP_Query($args):new WP_Query;
       		return $query;
       	}
   
       }
       ```
   

The topic ‘How to modify Related Posts function’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/hueman/3.7.27/screenshot.png)
 * Hueman
 * [Support Threads](https://wordpress.org/support/theme/hueman/)
 * [Active Topics](https://wordpress.org/support/theme/hueman/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/hueman/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/hueman/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [Uli](https://wordpress.org/support/users/uli/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/how-to-modify-related-posts-function/)
 * Status: not resolved