How to show related posts of Custom post Type


Related Posts for custom post type

Related Post – Custom Post Type

Custom post are quite common in WordPress, they are very much there to enhance the working of WordPress based website.  But it is a common problem to show related post of custom post type as right now there is no plugin available in WordPress plugin repository which specifically deals with custom post type.

So in this tutorial, we are going to show you how you can define a custom post “affiliate product”  with some custom taxonomy and later on using the same taxonomy, we will show the related posts.

Extra bytes

As you are well-aware that to show this custom post type, we need special query to include this post in our index.php file or to include this in rss feed.  You can see our post on how to include custom post so that all published post automatically publicize in all social media network by jetpack. How to add Cusstom post Type in Jetpack

By default single.php file is designed to display all types of post if user request as a single post but that is too common, if you have some custom taxonomy or custom fields then it would be not sufficient at all. Template hierarchy guide us to define a file single-{customePost}.php

$custom_taxterms = wp_get_object_terms( $post->ID, 'Occasion', array('fields' => 'ids') );

The above code will find all the terms associated with custom Taxonomy “Occasion”

Now, we are supposed to create a new wp_query that will fetch the data from WordPress database, here is the custom arguments

The whole code is divided into three major parts. First of all we are going to take all the terms associated with that post having that taxonomy.

$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 12, // you may edit this number
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Occasion',
'field' => 'id',
'terms' => $custom_taxterms
)
),
'post__not_in' => array ($post->ID),
);

I think the arguments are self explanatory so no need to explain.

Now the major part that will fetch data from WordPress database, I would here suggest you to please check wp_query and looping for better understanding of this part

$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
while ( $related_items->have_posts() ) : $related_items->the_post();?>
<div class="col-md-3 col-sm-4 col-xs-12">
<div class="panel pandel-default">
<a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(300,200), array('class'=>'img-responsive') );?></a>
<div class="panel-footer">
<h4 style="width:100%;overflow:hidden;"><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h4>
<?php the_excerpt();?>
</div>
</div>
</div>
<?php
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>

Now the whole source code for related posts of custom post Type looks like this.

<?php
$custom_taxterms = wp_get_object_terms( $post->ID, 'Occasion', array('fields' => 'ids') );
// arguments
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 12, // you may edit this number
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Occasion',
'field' => 'id',
'terms' => $custom_taxterms
)
),
'post__not_in' => array ($post->ID),
);
$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
while ( $related_items->have_posts() ) : $related_items->the_post();?>
<div class="col-md-3 col-sm-4 col-xs-12">
<div class="panel pandel-default">
<a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(300,200), array('class'=>'img-responsive') );?></a>
<div class="panel-footer">
<h4 style="width:100%;overflow:hidden;"><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h4>
<?php the_excerpt();?>
<p>
<span style="text-align:left;font-size:12px;">$ <?php echo rwmb_meta('price');?></span> <span class="check-now" style="float:right;"><a href="<?php echo rwmb_meta('link');?>" title ="via <?php echo rwmb_meta('site_name');?>" target="_blank"> Check it now</a></span>
</p>
</div>
</div>
</div>
<?php
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>

Now, place this code inside your single-{customePostType}.php file. This code – related posts of custom post type is a part of our upcoming affiliate WordPress theme –Expression.

4 Brilliant Comments - Join Discussion Now!

  1. Michelle says:

    I recently start to do work on my blog. I was facing this issue of showing related articles on the timeline. Your suggestion works.
    Thanks !

    • rakesh says:

      Hi Michelle, happy to know that this help you to solve your issue. Keep in touch

  2. 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. Displaying the related post of the article seems very easy and now i can add related posts list to my blog without using any plugin.

    • rakesh says:

      Hi Sagar, Good to know that you like this code. It was really a very Good issue in front of me while developing my own Gift Gallery WordPress theme. keep in touch Sagar.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>