Add Related Posts to WordPress

Wordpress LogoGenerally speaking there are a few primary objectives for having a blog/website, get people to your site, provide quality information and keep people on your site.  One way to encourage people on your site is to provide an easy way for viewers to find information on your site that is related to what brought them there in the first place.

One popular way is to provide a list of related posts that are similar in content to the one they are looking at.  There are several WordPress Plugins that enable this feature.  If you are looking for a way to reduce your reliance on Plugins but want the same functionality, here is some code that can be added to your  themes single.php  that will add a list of related posts to your current post. The code creates a list of 5 posts that are related to the current post.  The relation is based from Tags in the post. An example of this related posts list can be found at the bottom of this post below the comments.

NOTE:  Before making any changes to your single.php, it is best practice to always create a backup of the file.  A quick and easy way is to open the file, copy all of the code and paste it in notepad.

  • Open your WordPress Dashboard
  • Go to  Appearance
  • Go to Editor
  • Click on single.php
  • Find the section where you would like the related posts to show up and copy the php code below: (I placed mine just before the Comments section of the post)

$original_post = $post;
$tags = wp_get_post_tags($post->ID);
echo ‘

Related Posts

$sendTags = array();
foreach($tags as $tag)
$sendTags[] = $tag->term_id;

$args = array(
‘tag__in’ => $sendTags,
‘post__not_in’ => array($post->ID),
‘showposts’ => 5,
‘caller_get_posts’ => 1,

$queryDb = new WP_Query($args);
echo ‘

$post = $original_post;

  • Save the file.
  • Open a post and test.

Thanks to Chris Roane on for the code and information on how to implement it.

Leave a Reply

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


Subscribe without commenting