How To Display Related WordPress Posts Without Plugins

There are many ways to display related posts on WordPress without using unnecessary related posts plugins that are notorious for slowing down WordPress websites. In this article we will discuss how to display related posts on a list per category and tag using simple PHP that can be implemented into theme files such as single.php, category.php, and others.

Display Related Posts In Same Category

To show related posts on WordPress by category, copy and paste the code below into a PHP template file.

<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
 $tag_ids = array();
 foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

 $args=array(
 'tag__in' => $tag_ids,
 'post__not_in' => array($post->ID),
 'showposts'=>5, // Number of related posts that will be shown.
 'caller_get_posts'=>1
 );
 $my_query = new wp_query($args);
 if( $my_query->have_posts() ) {
 echo '<h3>Related Posts</h3><ul>';
 while ($my_query->have_posts()) {
 $my_query->the_post();
 ?>
 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
 <?php
 }
 echo '</ul>';
 }
}
?>

To add related posts to published WordPress posts automatically paste this code to your preferred location on the single.php file.

Display Related Posts Using The Same Tags

To display related posts per associated tag used, copy and paste the code below into a template file.

<?php
$categories = get_the_category($post->ID);
if ($categories) {
 $category_ids = array();
 foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

 $args=array(
 'category__in' => $category_ids,
 'post__not_in' => array($post->ID),
 'showposts'=>5, // Number of related posts that will be shown.
 'caller_get_posts'=>1
 ); 
 $my_query = new wp_query($args);
 if( $my_query->have_posts() ) {
 echo '<h3>Related Posts</h3><ul>';
 while ($my_query->have_posts()) {
 $my_query->the_post();
 ?>
 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
 <?php
 }
 echo '</ul>';
 }
}
?>

Changing The Amount Of Related Posts Displayed

To change the amount of posts displayed switch the 5 on line 10 to your preferred number.

 'showposts'=>5, // Number of related posts that will be shown.

Adding Some Randomness

To make related posts refresh in a more random manner insert the extra code below directly above caller_get_posts.

'orderby'=> rand,

Here’s an example:

$args=array(
 'tag__in' => $tag_ids,
 'post__not_in' => array($post->ID),
 'showposts'=>3, // Number of related posts that will be shown.
 'orderby'=> rand,
 'caller_get_posts'=>1
 );

Sean Doyle

http://Botcrawl.com

Sean Doyle is an engineer from Los Angeles, California. Sean's primary focuses include Internet Security, Web Spam, and Online Marketing.

Comment ( 1 )

  1. ReplyGurmeet
    Hello, Sean Davis, This is very helpful for me...but here you have added the Category code in tags section and Tag code in Category section. Please change it. Thanks a lot!