How to Show Related Posts for Custom Post Type in WordPress
Related posts in WordPress is the very useful to engage the audience of your website. WordPress gives you the capability to show the related posts of a specific post type. Display the related post of the article is very easy and you can add related posts list to your blog without using any plugin.
If you want to show related posts for custom post type, the posts need to be fetched by custom taxonomy terms of custom post type. In this tutorial, we will show how you can easily display the related posts for custom post type in WordPress. You don’t need to use any WordPress plugin for showing the related post of custom post type.
Generally, specific post details page (single.php) is used for custom post type. Open the
single-custom_post_type.php file and place the following code where you want to display the related posts list of custom post type. This code will get posts of the same custom post type and same custom taxonomy terms of the current single post.
//get the taxonomy terms of custom post type
$customTaxonomyTerms = wp_get_object_terms( $post->ID, 'your_taxonomy', array('fields' => 'ids') );
$args = array(
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'rand',
'tax_query' => array(
'taxonomy' => 'your_custom_taxonomy',
'field' => 'id',
'terms' => $customTaxonomyTerms
'post__not_in' => array ($post->ID),
$relatedPosts = new WP_Query( $args );
//loop through query
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
//no posts found
//restore original post data
Specify the custom post type in
post_type and custom taxonomy term of that post type in
taxonomy. Also, specify the number of related posts in
posts_per_page that you want to show.