DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Displaying related posts is a very great way to help visitors staying longer on your blog. You can use a plugin, but you also can use tags and a custom code to show related posts. Let’s see how to do!

This code will display related posts based on the current post tag(s). It must be pasted within the loop.

<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  echo 'Related Posts';
  $first_tag = $tags[0]->term_id;
  $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'showposts'=>5,
    'caller_get_posts'=>1
   );
  $my_query = new WP_Query($args);
  if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
      <?php
    endwhile;
  }
}
?>

Thanks to MichaelH for this awesome piece of code!

27 Comments

  1. Not work on WordPress 3.2.1 🙂

  2. As of today, it’s working great with my site.
    Oh, and I’m using WordPress 3.2.1.
    Thanks!

    Now, if only it would show a thumbnail of each post, it’d be 100%.

  3. You can use thumbnail by including this in your loop:

  4. Sorry – that code has come out completely messed up.
    you can see the full text at
    http://photos-for-you.com/single.php.txt

  5. Thanks for the great piece of code! Via the WordPress forum, I found some extra code to adapt it for showing multiple tags. However, I’m running into a problem I cannot fix: I’m using posts with several tags. The effect is that some posts are turning up twice in the results. I’ve checked the loop page on WordPress, but cannot get the code right. Do you have any idea on how I can fix this? You can see the code I’m using here:
    http://pastebin.com/QaUjX1u5

  6. Why only with Tags? Why not with categories? I mean, if anyone can update the code to display related posts by category, would appreciate it a lot. Thanks for any help you can provide.

  7. Not work on WordPress 3.2.1 – +1

  8. It does not work in the latest version of WP

  9. You should add wp_reset_query(); at the end to reset custom loop created and back to the main loop.

    This snippet works fine on WordPress 3.2.1. Make sure you put it in the loop.

  10. Works on version 3.3.1!!! Thanks.

  11. Works on version 3.3.1 Thank mate

  12. Thanks for the code MichaelH ….:)

  13. thank you for this code

    can u tell me how to make it work with category instead of tags?

  14. Put this in. Now my website doesn’t show up and I can’t get to my wp-admin dashboard. Help.

  15. I have been attempting to use the YARPP but continually get errors. Not sure what’s wrong… I’ve been thinking about going manually. I appreciate your post. I am still trying to figure out the CSS but this is a point in the right direction. Thanks!

  16. thanks for this code. i want to related category post. can you give me this code??

  17. As ArmNo said, it should have “wp_reset_query();” in the end, just like that:

    }
    }
    wp_reset_query();
    ?>

    Without that, the Disqus (comment system) was getting the comment from the post inside that code 🙂

  18. It doesn’t work for word press 3.4.2

  19. same as handelsregistera here.! it doesn’t work
    but i had another simple methods. 😀

  20. Sir, Article is Nice… I want to know that whether it is possible to show list of posts with same tag in sidebar sorted by published date.

  21. Awesome.
    Thanks for sharing 🙂

  22. working code for 3.4:

    //for use in the loop, list 5 post titles related to first tag on current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    echo 'Related Posts';
    $args=array(
    'tag_in' => array($tags),
    'post_not_in' => array($post->ID),
    'showposts' => 5,
    'ignore_sticky_posts' => 1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <a href="" rel="bookmark" title="Permanent Link to ">
    <?php
    endwhile;
    }
    }

  23. not work on ver 3.5.1

  24. Where to paste this code. How can i use this code . I am a newbie Please illustrate with example ?

  25. How about adding thumbnails? What is the code for that?

  26. Wow, it worked on WP 3.5.2 too. Anyway is there any trick to show thumbnails as well?

  27. Is there a way to display related post for all tags?

Leave a Comment

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