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:
- 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!
- Being a better plugin citizen: now it doesn’t require the user to click some sketchy button to
alterthe database and enable afulltext key. Usingregister_activation_hook, it does it automagically on plugin activation. Just install and go! - 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)
- 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).
- 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)
Related posts:
- Yet Another Related Posts Plugin 2.0
- Keep up with Yet Another Related Posts Plugin with RSS!
- Using Templates with YARPP 3
- This is what a release looks like
- Modifiying WordPress plugin activation behavior
Related posts brought to you by Yet Another Related Posts Plugin.
Tags: algorithm, code, PHP, plugin, threshold, WordPress, WordPress Planet, YARPP
If you enjoyed this post, make sure you subscribe to my RSS feed (optionally with tweets from my Twitter)!
January 3rd, 2008 at 2:17 pm
It would be great if it could offer also relevant PAGES not only posts
January 3rd, 2008 at 2:20 pm
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)
January 3rd, 2008 at 3:01 pm
@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
January 5th, 2008 at 3:34 am
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!January 5th, 2008 at 3:40 am
@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 insingle.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.January 5th, 2008 at 3:40 am
Oops, the php code snippet was stripped out from my comment above. Unless it was obvious, I referred to the related_posts() function call.
January 5th, 2008 at 3:45 am
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
January 5th, 2008 at 10:39 am
[…] 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 […]
January 5th, 2008 at 2:04 pm
@sairuh The bug is fixed in the latest version: 1.0.1.
January 5th, 2008 at 6:19 pm
Yep, v1.0.1 did the trick. Thanks so much!
January 6th, 2008 at 9:07 pm
[…] (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 […]
January 8th, 2008 at 3:39 pm
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(); ? >
What have I done wrong? Thanks!
January 8th, 2008 at 5:17 pm
@Nicole Hm,
theloop.phpappears 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?January 12th, 2008 at 10:08 am
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?
January 12th, 2008 at 1:06 pm
@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: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.
August 1st, 2008 at 7:40 am
[…] 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 […]
April 7th, 2009 at 7:21 am
You don’t need to use templates to do this… you can just add a CSS class to the
before each related postoption (for example,<li>><li class='relatedpost'>) and then add some CSS declarations for that class (e.g.,relatedpost).April 28th, 2009 at 3:11 pm
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?