Query to show related post

We developer survive each and everyday and always spent a lot of time in google to find a simple solution for our day to day problem.

Show related post in a single post is one of them. Today I also spent more than one hour in google to find this solution for a custom post type.

For this type of solution we need to write taxonomy query along with other query arguments and also we need category Id of that specific single post type. So that, we can that type of category post too.

We will write a function to achieve this functionality. Lets get our hand dirty.

At first we need to make sure we know the taxonomy slug for that custom post type.

$post_taxonomy = 'your-cpt-taxonomy-name';
$cat_ids = array();
$categories = get_the_terms($post_id, $post_taxonomy);
if(!empty($categories) && ! is_wp_error($categories)):
	foreach ($categories as $category):
		array_push($cat_ids, $category->term_id);
	endforeach;
endif;

Now we have array of category id for the respective post.

We need to set our query arguments like below.

$query_args = array( 
'post_type'      => get_post_type($post_id),
'post__not_in'    => array($post_id),
'posts_per_page'  => 10,
'tax_query' => array(
	array(
		'taxonomy' => $post_taxonomy,
		'field' => 'id',
		'terms' => $cat_ids
	)
)
);

We need to pass our arguments inside WP_Query.

$related_cats_post = new WP_Query( $query_args );

If we have posts for the specific custom post type, then ten post will appears inside $related_cats_post variable.

We need to loop through every post that we get from WP_Query.

if($related_cats_post->have_posts()):
	while($related_cats_post->have_posts()):
		$related_cats_post->the_post()
		?>
			<li>
				<a href="<?php echo esc_url(the_permalink())?>">
					<?php the_title();?>
				</a>
			</li>
		<?php
	endwhile;
	wp_reset_postdata();
endif;	 

So thats all need to set inside our function.

Below is the full code of that function.

function my_related_expert_content($post_id){
	$post_taxonomy = 'your-cpt-taxonomy-name';
	$cat_ids = array();
	$categories = get_the_terms($post_id, $post_taxonomy);
	if(!empty($categories) && ! is_wp_error($categories)):
		foreach ($categories as $category):
			array_push($cat_ids, $category->term_id);
		endforeach;
	endif;
	
	$query_args = array( 
		'post_type'      => get_post_type($post_id),
		'post__not_in'    => array($post_id),
		'posts_per_page'  => 10,
		'tax_query' => array(
			array(
				'taxonomy' => $post_taxonomy,
				'field' => 'id',
				'terms' => $cat_ids
			)
		)
	);
	$related_cats_post = new WP_Query( $query_args );
	if($related_cats_post->have_posts()):
		while($related_cats_post->have_posts()):
			$related_cats_post->the_post()
			?>
				<li>
					<a href="<?php echo esc_url(the_permalink())?>">
						<?php the_title();?>
					</a>
				</li>
			<?php
		endwhile;
		wp_reset_postdata();
	endif;	 
}

Now we can call this function any where inside theme and pass a post id to get related post for that specific post.

Thanks.