mitcho Michael 芳貴 Erlewine

Linguist. Fifth year PhD student at MIT.

blog

Yet Another Related Posts Plugin

UPDATE:

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 wordpress.org. If you have questions, please submit on the wordpress.org forum. Thanks!

Description

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.

Installation

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.

Examples

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:
<ol>
<?php related_posts(); ?>
</ol>
</p>
<?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)!

18 Responses to “Yet Another Related Posts Plugin”

  1. Tomáš Kapler Says:

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

  2. Tomáš Kapler Says:

    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)

  3. mitcho Says:

    @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

  4. sairuh Says:

    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!

  5. mitcho Says:

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

  6. sairuh Says:

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

  7. sairuh Says:

    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

  8. mitcho.com > blog > Modifiying WordPress plugin activation behavior Says:

    […] I continue to work on and debug Yet Another Related Posts Plugin and WP-Smartdate, I’ve come across an issue where plugin activation fails, but I get no […]

  9. mitcho Says:

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

  10. sairuh Says:

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

  11. Iwaruna.com » Poke tweak prod tweak Says:

    […] (6-Jan-2008): Thanks to mitcho’s Yet Another Related Posts Plugin (YARPP), individual article pages now contain a list of related entries. This plugin, available at […]

  12. Nichole Says:

    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!

  13. mitcho Says:

    @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?

  14. jan Says:

    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?

  15. mitcho Says:

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

  16. Replacing the related posts plugin in Wordpress Says:

    […] because they’ll get to see a small insight on the post. So I saw a new plugin called as Yet another related posts plugin. There are a lot of features in it but the major thing that I liked was the Match Threshold […]

  17. mitcho Says:

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

  18. krsnsk Says:

    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?

Leave a Reply

mitcho.com uses Markdown with Smarty Pants for simple, lightweight markup.