WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Display Related Posts in WordPress

How to Display Related Posts in WordPress

Last updated on March 4th, 2013 by Editorial Staff
53 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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

53 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

67 Comments

Leave a Reply
  1. Marcel says:
    Mar 29, 2019 at 3:59 pm

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

    Reply
    • WPBeginner Support says:
      Apr 1, 2019 at 11:29 am

      For adding the thumbnail, you would want to take a look at our article here: https://www.wpbeginner.com/wp-themes/how-to-add-related-posts-with-a-thumbnail-without-using-plugins/
      For a custom taxonomy, depending on how you set it up, you would want to use wp_get_post_terms instead of wp_get_post_tags

      Reply
  2. HAMID says:
    Feb 25, 2019 at 10:52 am

    yet another related posts plugin is no longer available for download.
    SO WHAT IS THE ALTERNATIVE?

    Reply
    • WPBeginner Support says:
      Feb 26, 2019 at 1:13 pm

      Thank you for letting us know about the plugin being unavailable, we have a list of alternatives here: https://www.wpbeginner.com/plugins/5-best-related-posts-plugins-for-wordpress/

      Reply
  3. Bill says:
    Sep 18, 2018 at 8:06 am

    Nice article. But how can I show the latest ones?

    Reply
    • WPBeginner Support says:
      Sep 18, 2018 at 12:53 pm

      Hi Bill,

      Please see our article on how to show recent posts in WordPress.

      Reply
  4. jack says:
    Sep 1, 2018 at 12:27 pm

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

    Reply
  5. Fintan says:
    Mar 31, 2018 at 5:41 am

    If I use this code, will it open the posts up in a new window or the same window.

    Reply
  6. Mohammed Khaled says:
    Jan 9, 2018 at 8:46 am

    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);

    Reply
    • Scortt says:
      Feb 3, 2018 at 2:30 pm

      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.

      Reply
  7. andi says:
    Jul 6, 2017 at 1:51 pm

    i added this code in single.php i wordpress theme but not work

    Reply
  8. Judy Hicks says:
    Apr 20, 2017 at 10:40 pm

    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!

    Reply
  9. Aamir says:
    Jan 6, 2017 at 11:28 am

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

    Reply
    • Aamir shahzad says:
      Aug 21, 2017 at 5:03 am

      You can do this with shortcode

      Reply
  10. dineshkashera says:
    Jan 5, 2017 at 5:33 am

    nice job, code working fine.
    thanks

    Reply
  11. Mike G says:
    Oct 19, 2016 at 3:40 pm

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

    Reply
    • Paranoya says:
      Jul 7, 2017 at 5:07 am

      This is it

      Reply
  12. Mike says:
    Jul 29, 2016 at 7:25 am

    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

    Reply
  13. Sumit Kumar Gogawat says:
    Jun 15, 2016 at 12:55 am

    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.

    Reply
    • WPBeginner Support says:
      Jun 15, 2016 at 8:47 pm

      See our guide on how to add inline related posts in WordPress.

      Reply
  14. Kyler Boudreau says:
    Mar 7, 2016 at 9:31 pm

    Thanks for this info! Exactly what I needed.

    Reply
  15. JMunce says:
    Dec 31, 2015 at 12:50 am

    I don’t get ‘related posts’ from this code.

    I just get recent posts

    Reply
  16. Debb says:
    Dec 24, 2015 at 12:03 pm

    How about second tag? Can i change $first_tag to $second_tag?

    Reply
  17. Ben Chung says:
    Nov 26, 2015 at 9:38 am

    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.

    Reply
    • Ben Chung says:
      Nov 26, 2015 at 9:39 am

      Sorry, I meant, replace it with:
      ‘ignore_sticky_posts’ => 1

      Reply
  18. David says:
    Oct 13, 2015 at 12:50 pm

    This is great. Please direct me to how I can add a post image and date. Thanks!

    Reply
  19. Sam says:
    Sep 18, 2015 at 8:13 am

    Appreciate the tutorial! Very simple to implement.

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

    Thanks

    Reply
  20. Allie says:
    Aug 21, 2015 at 11:44 am

    I am also using Genesis and there is not single.php form. Where do I input this? Thanks!

    Reply
    • Chris Arter says:
      Aug 28, 2015 at 3:26 pm

      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..

      Reply
      • Chris Arter says:
        Aug 28, 2015 at 3:27 pm

        Whoops, typo above. Should be is_singular

        Reply
  21. Jornes says:
    Mar 9, 2015 at 2:55 pm

    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?

    Reply
  22. Dave Noel says:
    Nov 10, 2014 at 11:08 pm

    You can insert ads at runtime using jquery. That’s what I do.

    Reply
  23. Clay Teller says:
    Aug 8, 2014 at 4:24 pm

    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’
    );

    Reply
    • Sam says:
      Sep 18, 2015 at 8:11 am

      Thanks Clay, useful!

      Reply
  24. Szyam says:
    Jun 16, 2014 at 4:12 pm

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

    Reply
  25. Russ Edwards says:
    Apr 15, 2014 at 11:05 am

    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?

    Reply
  26. Tom Nguyen says:
    Feb 24, 2014 at 10:34 am

    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.

    Reply
  27. Sid says:
    Jul 10, 2013 at 10:42 pm

    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?

    Reply
  28. Danny says:
    May 23, 2013 at 11:47 am

    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!

    Reply
  29. Eline says:
    May 4, 2013 at 4:12 pm

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

    Reply
    • Editorial Staff says:
      May 9, 2013 at 11:23 am

      Our tutorials are only for self-hosted blogs.

      Reply
  30. Rodrigo Ghedin says:
    Apr 24, 2013 at 12:27 pm

    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!

    Reply
    • s.i says:
      Nov 2, 2014 at 7:20 pm

      is easiest if use has_tag insted of $tags

      if (has_tag( ‘mytag’ ))

      else

      Reply
  31. Nadine says:
    Mar 4, 2013 at 2:18 am

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

    Reply
    • Editorial Staff says:
      Mar 4, 2013 at 6:26 am

      Just updated the article Nadine. showposts parameter have been depracated with posts_per_page.

      Reply
  32. Vincentb says:
    Jul 14, 2011 at 11:42 pm

    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?

    Reply
  33. sara says:
    Dec 6, 2010 at 3:46 pm

    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

    Reply
    • Editorial Staff says:
      Dec 7, 2010 at 8:23 am

      Here is the article that you should look at:

      https://www.wpbeginner.com/wp-themes/how-to-add-related-posts-with-a-thumbnail-without-using-plugins/

      Reply
  34. will says:
    Nov 16, 2010 at 10:58 am

    very use full thanks installed the YARP plugin and it works better than i thought it would thanks.

    Reply
  35. Abhijit V. Chaore says:
    Jun 11, 2010 at 7:11 am

    Sounds quite useful. Thanks for the information.

    Reply
  36. Paisakamana says:
    Jun 8, 2010 at 11:30 am

    Really great piece of code you got there.
    Great article I used this on my site and I like.

    Reply
  37. brynh says:
    Mar 3, 2010 at 1:14 pm

    Very good article! It helped me much! Thank you.. I installed YARP and it works great..

    Reply
  38. Chris Roane says:
    Feb 17, 2010 at 3:20 pm

    Thanks for providing the code. I needed something that was similar to this, but that took into account all of the tags for a post to generate the related posts.

    So I modified the code and posted it on my website. I did put a link to this page in the article. Here is the article that I wrote: http://www.montanaprogrammer.com/php-web-programming/add-related-posts-to-wordpress/

    Reply
    • Taylor says:
      Feb 28, 2014 at 10:59 am

      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!

      Reply
  39. Vincent says:
    Feb 16, 2010 at 1:21 am

    (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.

    Reply
    • Editorial Staff says:
      Feb 16, 2010 at 2:16 am

      Yes you can. Use this code and the tag version. Display WordPress Related Posts without a plugin

      Reply
      • Vincent says:
        Feb 16, 2010 at 2:37 am

        Gee, thanks for the fast response!

        I’ll give that code a whirl.

        Reply
  40. matthew says:
    Jan 26, 2010 at 8:04 pm

    Great tip! thanks for sharing this

    Reply
  41. Dario Gutierrez says:
    Nov 3, 2009 at 7:31 pm

    Excellent, buy how can i put a thumbnail? Thanks.

    Reply
  42. Sal Surra says:
    Oct 16, 2009 at 6:52 pm

    Great tip! Works perfectly and thanks for sharing this. I like the tip as it shows my recent posts in a nice lists view.

    Reply
  43. palmamod says:
    Sep 9, 2009 at 12:30 pm

    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!

    Reply
    • Editorial Staff says:
      Sep 9, 2009 at 4:12 pm

      Simply add the < li > tag around the a href tag. the link tag.

      Reply
  44. Vin says:
    Aug 30, 2009 at 6:56 pm

    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?

    Reply
    • Editorial Staff says:
      Aug 30, 2009 at 9:52 pm

      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.

      Reply
  45. Sergej Müller says:
    Aug 30, 2009 at 3:59 am

    See also my plugin for related posts by category.

    Reply
  46. Ileaneb says:
    Aug 29, 2009 at 8:17 am

    Does this work for wordpress.com as well? Are the instructions the same?

    Reply
    • Editorial Staff says:
      Aug 29, 2009 at 3:18 pm

      If you can install the plugin, then yes it will work. If you can change the codes in theme then it will work.

      Reply

Leave a Reply Cancel 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.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
SeedProd Logo
SeedProd
Create beautiful custom landing pages - Drag & drop builder. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
Churnly Coupon
Get 30% OFF on Churnly WordPress automated churn-busting plugin.
AccessPress Themes
AccessPress Themes Coupon
Get 15% off on AccessPress Themes collection of premium WordPress themes and plugins.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.