Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Display Related Posts in WordPress

If you know anything about bounce rate, you are probably already showing related posts in WordPress. Bounce rate essentially represents the percentage of initial visitors to a site who “bounce” away to a different site, rather than continue on to other pages within the same site. This can have a significant impact on your adsense earning. By adding Related posts in WordPress you are killing three birds with one stone. You are decreasing your bounce rate, you are increasing your adsense income, and lastly you are increasing your pageviews. In this article we will show you how you can display related posts in WordPress with and without a plugin.

There are two methods of doing so and you can choose whichever method you prefer. One way is doing it without a plugin and then there is another method that is doing it with a plugin.

Display Related Posts in WordPress without a Plugin

Paste the following code in single.php where you want to display related posts

<?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),
'posts_per_page'=>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(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

<?php
endwhile;
}
wp_reset_query();
}
?>

Source: MichaelH

Display Related Posts in WordPress with a Plugin

The plugin we use is called Yet Another Related Post Plugin (YARP). It gives you a list of posts and/or pages related to the current entry, introducing the reader to other relevant content on your site. Key features include:

  • An advanced and versatile algorithm: Using a customizable algorithm considering post titles, content, tags, and categories, YARPP calculates a “match score” for each pair of posts on your blog. You choose the threshold limit for relevance and you get more related posts if there are more related posts and less if there are less.
  • Templating: New in 3.0! A new templating system puts you in charge of how your posts are displayed.
  • Caching: New in 3.0! YARPP now caches the related posts for all the posts and thus has significantly improved performance over versions 2.x.
  • Related posts in RSS feeds: Display related posts in your RSS and Atom feeds with custom display options.
  • Disallowing certain tags or categories: You can choose certain tags or categories as disallowed, meaning any page or post with such tags or categories will not be served up by the plugin.
  • Related posts and pages: Puts you in control of pulling up related posts, pages, or both.

Download YARP

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

69 CommentsLeave a Reply

  1. Very nice article but how can this be done with custom taxonomies? And can I add featured images of the related posts?

  2. is there any way that i show visitors a code which shows my related post or recent post in their websites?

  3. Hey,
    Thank you for this useful post,
    but i need to know What is the -ID- in line below return to…
    $tags = wp_get_post_tags($post->ID);

    • Mohammed, $post is a variable that is holding a WordPress object. The object contains many things, one of which is the id number of the post.

  4. I have set my posts to display recent posts from the post category. When I have a post with more than one category, I get 2 instances of recent posts. Is there a way to tell the plugin to limit to the first category instance? thanks!

  5. Is there any way to show the related post in between the blog post, I mean somewhere middle of the post, as per choice.

  6. What plugin allows to display related post/pages on pages by “manually” selecting the pages that are related? Thank you

  7. Hi, thanks for your awesome website!

    I have a question and would be grateful if your answer it; I have problem with both ways in this post regarding displaying relatedpost in my blog, if I add the related posts to the end of each post manually, then is that ok in terms of seo (or anything else)?

    Kind regards,
    Mike

  8. Great Post!

    I want to know how to add Related post right after first paragraph or middle in post in wordpress. can you please update me.

  9. Thanks for the great tip.
    One thing I noticed is that I got an error when running it:
    “WP_Query was called with an argument that is deprecated since version 3.1! “caller_get_posts” is deprecated. Use “ignore_sticky_posts” instead. ”

    So replacing:

    ‘caller_get_posts’ => 1

    with:

    ‘caller_get_posts’ => 1

    fixed the problem.
    Thanks.

  10. Appreciate the tutorial! Very simple to implement.

    One question, does this only match articles by matching their first tag?

    Thanks

    • This code can be put in your functions file depending on what location of the page you’d like. for instance..

      add_action(‘genesis_after_entry_content’,’myRelatedPosts’);

      if (is_singluar(‘post’)) {

      // your related posts code here

      }

      The best place to put this is in your own plugin. Try not to edit your Functions file..

  11. Hi! I did apply this code to my website sidebar. But, i am wondering why it doesn’t work after i have implemented a php code that calls this function to work when the page is single.php. Do you know what is going wrong?

  12. Thanks for this useful bit of code! I think ordering the results randomly can be helpful for some sites to keep the related posts “fresh”. Can be done by adding ‘orderby’=>’rand’ argument like so:

    $args=array(
    ‘tag__in’ => array($first_tag),
    ‘post__not_in’ => array($post->ID),
    ‘posts_per_page’=>5,
    ‘caller_get_posts’=>1,
    ‘orderby’=>’rand’
    );

  13. Very nice! Looks like ‘caller_get_posts’ Now needs to be ‘ignore_sticky_posts’ instead. Works great.

  14. Works great, but how would I split the results. I want to put an ad in between, so it would be, for example, 5 related posts -then ad- next five related posts, etc. i know there should be an “offset” somewhere in the second query, just not sure how or where it insert. help?

  15. Thanks for posting both ways of doing this. I prefer not to use another plugin. I’m going to try on my website since I have more than 20 blog posts.

  16. I’m using Genesis and you know, there’s no separate single.php file. So, where should I paste this? In functions file? Also, is there something to edit at stylesheet?

  17. Whoop! Whoop! Thanks so much for this code, I have been search for about a week, and this works just great once you style it.

    Thanks alot!

  18. I don’t know how this work, and on linkwithin it doesn’t work, my blog isn’t slef-hosted! Help:(

  19. I’m using and enjoying the manual solution. But I miss a conditional trigger for situations when there aren’t related posts. Right now it returns nothing. How could I fill that space with a “there’s no related posts” when… well, when there’s no related posts?

    Thanks!

  20. I’m trying to change ‘showposts’ to 12 but it only shows up 2 items. Any ideas why? :/

  21. Can you please add some more detail to your explanation? What do you mean by ‘tag version”? What file do I add the code above to (page.php?) and what do I change in that code to make it do what I described above?

    Thanks.

    Serg, your plugin only works on Posts, is there a way to make it work on Pages too?

  22. I’m looking for something similar to this but based on categories. I would like to only display related articles from the current category.

    I have searched for a solution but can’t seem to find anything, any idea’s?

    Thanks

    • Thank you for linking to this! I was looking for a way to show related posts based on all tags and not just the first one.

      Thanks!

  23. (revised comment)
    I’ve been looking for a way of adding POSTS related to specific tags on the bottom of PAGES I have created. EG: POSTS that are related to the pyramid of Teti…
    http://www.talkingpyramids.com/tag/teti/

    …on the bottom of the Pyramid of Teti PAGE:
    http://www.talkingpyramids.com/saqqara/pyramid-of-teti/

    At the moment I’ve just added the tags at the bottom of the PAGE but would be much better to have exerpts of the related POSTS added.

    Is there a way I can adapt the code that you have given to do this?

    Thanks.

  24. Hey, I implemented the non-plugin version of this and am 99% happy with it. I know it’s not you who have developed it but maybe you could help. If you check the posts on my blog, you will see that 5 posts are displayed (matching the first tag of the post) but “in line”. Do you know how can I tweak this so that a list occurs? Like, tweaking the php so that a and tags are created?
    Thanks a lot and keep up the good job!

  25. I’ve been looking for a way of adding posts related to specific tags on the bottom of pages I have created. EG: Posts that are related to the pyramid of Teti…
    http://www.pyramidofman.com/blog/tag/teti/

    …on the bottom of the Pyramid of Teti page:
    http://www.pyramidofman.com/blog/saqqara/pyramid-of-teti/

    At the moment I’ve just added the tags at the bottom of tghe page but would be much better to have exerpts of the related posts added.

    Is there a way i can adapt the code that you have given to do this?

    Thanks.

    @Sergej,
    Do you have a link?

    • Cannot access your site. Database connection error.

      For Sergej, use the link associated with his name. It takes you to his website and the link to the plugin is there.

      Admin

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.