In this article I’ll show you how to display related posts in WordPress. also talk about how to display Related Posts by Category and Tags
Related post functionality is an often desired feature for WordPress website owners. The related articles post widely used by many website owners . this trick uses for increase more interest to visit sites.
Why we are not using any plugin ..?
Plugin are extra thing and more heavier weight instead of code are more lighter .
Get started
here we describe how you can display default “post type” related post
<!--Start Related Posts-->
<?php
// Build basic custom query arguments
$custom_query = new WP_Query( array(
'posts_per_page' => 8, // Number of related posts to display
'post__not_in' => array($post->ID), // Ensure that the current post is not displayed
'orderby' => 'rand', // Randomize the results
));
// Run the loop and output data for the results
if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail( 'medium' ); ?></a>
<?php } ?>
<a href="<?php the_permalink(); ?>"><b><?php the_title(); ?></b></a>
<?php endwhile; ?>
<?php else : ?>
<p>Sorry, no related articles to display.</p>
<?php endif;
// Reset postdata
wp_reset_postdata();
?>
<!--End Related Posts-->
Related Posts by Category
If you want to display category base related posts then follow this code
// Get a list of the current post's categories
$catidlist = '';
foreach( $categories as $category) {
$catidlist .= $category->cat_ID . ",";
}
// Build our category based custom query arguments
$custom_query = new WP_Query(array(
'posts_per_page' => 8, // Number of related posts to display
'post__not_in' => array($post->ID), // Ensure that the current post is not displayed
'orderby' => 'rand', // Randomize the results
'cat' => $catidlist, // Select posts in the same categories as the current post
));
Related Posts by Tags
If you want to display tags base related posts then follow this code
// Get the tags for the current post
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
// If the post has tags, run the related post tag query
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
// Build our tag related custom query arguments
$custom_query_args=array(
'tag__in' => $tag_ids, // Select posts with related tags
'posts_per_page' => 8, // Number of related posts to display
'post__not_in' => array($post->ID), // Ensure that the current post is not displayed
'orderby' => 'rand', // Randomize the results
);
} else {
// If the post does not have tags, run the standard related posts query
$custom_query_args = array(
'posts_per_page' => 8, // Number of related posts to display
'post__not_in' => array($post->ID), // Ensure that the current post is not displayed
'orderby' => 'rand', // Randomize the results
);
}
// Initiate the custom query
$custom_query = new WP_Query( $custom_query_args );
Custom Post Type Related Posts
If you want to display related posts in a custom post type you’ve created, you can use the following code to get the job done. Simply change custom-post-type to the slug of the post type you would like to display.
// Build our custom post type custom query arguments
$custom_query = new WP_Query( array(
'post_type' => 'custom-post-type', // Select entries from a custom post type, input your slug here
'posts_per_page' => 8, // Number of related posts to display
'post__not_in' => array($post->ID), // Ensure that the current post is not displayed
'orderby' => 'rand', // Randomize the results
));
If you have any questions about related post, be sure to comment below.