mitcho Michael 芳貴 Erlewine

Postdoctoral fellow, McGill Linguistics.


Yet Another Related Posts Plugin


This posting is now outdated… for the latest information on YARPP, please visit YARPP’s very own page on my site, or its page on If you have questions, please submit on the forum. Thanks!


Today I’m releasing Yet Another Related Posts Plugin (YARPP1) 1.0 for WordPress. It’s the result of some tinkering with Peter Bowyer’s version of Alexander Malov & Mike Lu’s Related Entries plugin. Modifications made include:

  1. Limiting by a threshold: Peter Bowyer did the great work of making the algorithm use [[mysql]]’s fulltext search score to identify related posts. But it currently just displayed, for example, the top 5 most “relevant” entries, even if some of them weren’t at all similar. Now you can set a threshold limit2 for relevance, and you get more related posts if there are more related posts and less if there are less. Ha!
  2. Being a better plugin citizen: now it doesn’t require the user to click some sketchy button to alter the database and enable a fulltext key. Using register_activation_hook, it does it automagically on plugin activation. Just install and go!
  3. Miscellany: a nicer options screen, displaying the fulltext match score on output for admins, an option to allow related posts from the future, a couple bug fixes, etc.


Just put it in your /wp-content/plugins/ directory, activate, and then drop the related_posts function in your WP loop. Change any options in the Related Posts (YARPP) Options pane in Admin > Plugins.

You can override any options in an individual instance of related_posts using the following syntax:

`related_posts(limit, threshold, before title, after title, show excerpt, len, before excerpt, after excerpt, show pass posts, past only, show score);

Most of these should be self-explanatory. They’re also in the same order as the options on the YARPP Options pane.

Example: related_posts(10, null, 'title: ') changes the maximum related posts number to 10, keeps the default threshold from the Options pane, and adds title: to the beginning of every title.

There’s also a related_posts_exist) function. It has three optional arguments to override the defaults: a threshold, the past only boolean, and the show password-protected posts boolean.


For a barebones setup, just drop <?php related_posts(); ?> right after <?php the_content() ?>.

On my own blog I use the following code with <li> and </li> as the before/after entry options:

<?php if (related_posts_exist()): ?>
<p>Related posts:
<?php related_posts(); ?>
<?php else: ?>
<p>No related posts.</p>
<?php endif; ?>

Coming soon (probably)

  1. Incorporation of tags and categories in the algorithm. I’ve gotten the code working, but I still need to think about what the most natural algorithm would be for weighing these factors against the mysql fulltext score currently used (and works pretty well, I must say).
  2. Um, something else! Let me know if you have any suggestions for improvement. ^^

Version log

1.0 Initial upload (20071229)

1.0.1 Bugfix: 1.0 assumed you had Markdown installed (20070105)

  1. Pronounced “yarp!”, kind of like this, but maybe with a little more joy:

  2. Did you know that threshold has only two h’s!? I’m incensed and just went through and replaced all the instances of threshhold in my code. It’s really not a thresh-hold!? 

Tags: , , , , , , ,

If you enjoyed this post, make sure you subscribe to my RSS feed (optionally with tweets from my Twitter)!

  • Tomáš Kapler

    It would be great if it could offer also relevant PAGES not only posts

  • Tomáš Kapler

    and also an oposite threshold - do not show if the relevancy is bellow some value (so it would not show always e.g. 5 articles but maybe 1 or 3, but really relevant)

  • mitcho

    @Tomáš Thanks for the pages idea… I’ll look into it. As for the threshold… that is exactly what this plugin does and why it is unique, but I see how my description was ambiguous. Fixed

  • sairuh

    Thanks for developing this plugin. I’ve been wondering about a more intelligent plugin for related posts that wasn’t limited to just tags in common —I look forward to when you’re able to incorporate that feature. :-)

    However, I haven’t been able to get v1.0 to work on my local test server. I’ve plopped within the Loop of my theme’s single.php (is that an acceptable location?). But as soon as that part of the code is reached, the page stops displaying. For example, if I place the function above the entry-meta section, that section, any comments, the footer and the sidebar fail to render. Thanks in advance for any suggestions!

  • mitcho

    @sairuh Thanks for the note! I’m sorry you’ve had trouble with the plugin… did you try the related_posts() code? If it’s in the Loop in single.php, there shouldn’t be any problems… is there an error being produced in the source? I would love to see what error comes up.

  • sairuh

    Oops, the php code snippet was stripped out from my comment above. Unless it was obvious, I referred to the related_posts() function call.

  • sairuh

    From the php log I got the following:

    PHP Fatal error: Call to undefined function markdown() in [path]/yet-another-related-posts-plugin/yarpp.php on line 68

  • Pingback: > blog > Modifiying WordPress plugin activation behavior()

  • mitcho

    @sairuh The bug is fixed in the latest version: 1.0.1.

  • sairuh

    Yep, v1.0.1 did the trick. Thanks so much!

  • Pingback: » Poke tweak prod tweak()

  • Nichole

    I’m having trouble getting this plugin to work. I downloaded it and dropped it in to wp-content/plugins, then I added it to theloop.php. And then nothing happened. Here’s what it looks like in theloop.php: < div class=”itemtext” > < ?php the_content(__(‘Continue reading’,’k2_domain’) . ” ‘” . the_title(”, ”, false) . “’”); ? > < ?php related_posts(); ? >

                < /div >

    What have I done wrong? Thanks!

  • mitcho

    @Nicole Hm, theloop.php appears to be a K2 specific thing, and I’m sadly not that familiar with K2. Another possibility, though, is that the plugin was not activated in the Plugins configuration screen?

  • jan

    Great tool!

    How about adding the option including the post title in “search” for related posts. And also giving the title a higher weight.

    More explanation The current situation is that I have 2 very similar and related posts.

    Post 1. New Audi A4 to launch this January Post2. More pictures of the new Audi A4 for January

    Your plugin does not see that these 2 posts are related. Because it does not look at the title (or I don’t know how to make it look at the title). If it did look at the title or if the title could be set to more weight / more importance. Then It would see that these titles are related.

    What do you think?

  • mitcho

    @jan The plugin does indeed compare the text in titles… you can adjust the weight of the title text in line 54 of yarpp.php:

    'title' => 2,

    changing this to 5 or so might help you out. (All of these routines are a vestige of the previous plugins on which YARPP is based… I’ll probably be rewriting these and making these options more transparent (when I work in category and tag comparison as well)).

    Another option is to try lowering the match threshold in the YARPP options pane. Try lowering it to like 3 or even lower, then go back to your Audi pages and see if they’re cross-relating. If you’re using the default options and you’re logged in, the match score will show up next to it and you can use this to readjust your match threshold.

  • Pingback: Replacing the related posts plugin in Wordpress()

  • krsnsk

    hi mitcho yarpp 3.0.2 give "Warning: Cannot modify header information - headers already sent by (output started at /home/content/…./wp-content/plugins/yet-another-related-posts-plugin/keywords.php:91) in /home/content/…./wp-includes/pluggable.php on line 850" that error when i post a message. how can i fix it?

  • mitcho

    You don’t need to use templates to do this… you can just add a CSS class to the before each related post option (for example, <li> > <li class='relatedpost'>) and then add some CSS declarations for that class (e.g., relatedpost).