Improved Wordpress related posts plugin

I’ve always liked the idea of having related entries shown - it’s a smart system of navigation and helps explore the knowledge available. Hence the first plugin I installed on this Wordpress-based blog was Related Posts by Alexander Malov & Mike Lu.

The aim behind the related posts plugin is to show similar entries already made on your blog, regardless of their category. Think of it as displaying search results before the reader has searched. That’s how useful it can be if it works.

However, the Wordpress plugin did not give very good results when used on this site. Upon investigating the code, I discovered it was only using the post’s URL to compare the current post with other entries. Since my weblog URLs are based on the page title, which on this site doesn’t always highlight keywords from the entry, this was unsurprisingly not proving effective.

The plugin provided the option for you to put hidden keywords in your post which would be used. However I didn’t want to go back and add keywords to my old posts. Not to mention I’d forget the standard keywords between writing posts ;) Tagging is a job for the computer, not a human…

So I’ve hacked up a modified version, which takes the post content, and calculates word frequency using weightings that can easily be modified (lines 57-62 of the code) for different parts of the post (eg URL, title, content). It then uses these details to match against the MySQL full-text index, (hopefully) returning more relevant results.

The script currently does not use any stemming algorithm or match against category IDs. Those are the most obvious features to add but the latter would require the MySQL query being reworked, and it was bedtime!

If you want to see the MySQL full-text ranking (as shown on this site next to each related posts link at present on this blog) to give you an idea of the certainty of each match being shown, uncomment line 161 in related-posts.php.

Download related-posts.php

The code is not supported and was hacked because I was curious about recommendation algorithms. Improvements or suggestions are welcome. You are free to use and modify this code.
Question: Is there some way to manage file uploads like above from within Wordpress? I’ve uploaded it manually as the ‘upload’ box on the “Write Post” screen said a php file wasn’t a valid image!

35 comments ↓

#1 John Pozadzides on 01.26.07 at 4:34 am

Peter,

Two quick things…

1) To answer your question, if you install the Mime-config plugin you should be able to add PHP files to the system so that you can upload them using the built in upload funcationality. :-)

2) On my site I’m using the Ultimate Tag Warrior plugin and I’ve gone to great pains to go back and tag all of my posts with key words. I wonder if there would be a way for your hack to also take these into consideration?

Personally, I think that words found in the title and the UTW tags should carry considerably more weight than other words in the content, but it sounds like a good idea for everything to be considered.

Finally, the link to the original Related Posts plugin in down, so I wonder if you could describe the usage of your version of the plugin?

Thanks,

John

#2 Peter Bowyer on 02.02.07 at 9:42 am

Thanks John, I’ll upgrade to 2.1 first and then install that plugin if they haven’t dealt with it in the core.

As to UTW, yes if the keywords are in a separate database field then it’ll be fine to put great weighting on them. If you look at the source of my modified plugin, there’s a lot of commenting around the weighting algorithm, which should help to adjust it. Mine is very crude (though less crude than the origianl) - FWIW the title should already have a double or triple weighting.

I’m afraid I’ve no clue about its usage - drop it with the original files in the plugins directory I imagine and then activate it. Sorry - I hope the original page comes back up!

Kind regards,
Peter

#3 Brian on 03.18.07 at 5:44 pm

Peter,

This plugin (written by a priest named Rob Marsh) seems to do what you are after:

http://rmarsh.com/plugins/similar-posts/

#4 Simon on 04.29.07 at 10:35 pm

You don’t say anywhere how to use the plug-in, it’s ok for the likes of me because I can look through your code and find the function I need to call. But for the casual blogger they wouldn’t have a clue. You need to modify your post with usage.

#5 Peter Bowyer on 04.29.07 at 10:48 pm

Simon,

I didn’t provide any usage instructions as they were on the original site, which the modified code was supposed to complement rather than replace. Now that site’s disappeared I’ll have a dig and see if I can find any…

#6 Simon on 04.29.07 at 11:00 pm

I can help you out there:

Usage of WP_Related-posts Plugin.

Default include, into your template file where you want the plugin to show. Edit the plugins default options under the plugins >> Related Posts Options in your wordpress admin window.

If you wish to override the default options when including in a template then use the following hierarchy when calling the function:

related_posts($limit=5, $len=10, $before_title = '', $after_title = '', $before_post = '', $after_post = '', $show_pass_post = false, $show_excerpt = false);

$limit = Number of related posts to return.
$len = Length of excerpt to show if excerpt is set to true.
$before_title = What to display before each related post, I use li tags as they allow for easy styling.
$after_title = Same as before title just after it.
$before_post = What to display before each excerpt.
$after_post = What to display after each excerpt.
$show_pass_post = Show passworded posts.
$show_excerpt = If set to true, show an excerpt of the post limited to the length set by $len.

Hope that helps :)

#7 Simon on 04.29.07 at 11:02 pm

Ah i forgot to remove the related_posts() wrapped in php include tags.

#8 Peter Bowyer on 05.04.07 at 8:15 am

Excellent - thanks Simon!

#9 MrCorey on 05.06.07 at 7:36 pm

Hi Peter. I just thought that I’d drop a quick note to you, as I’d been looking for an alternative to what I had been using. I noticed that no one has pointed out the new link for the original Related Posts plugin, so here it is:

http://wasabi.pbwiki.com/Related%20Entries

#10 Adam Dempsey on 05.11.07 at 3:58 pm

Just what I needed, thanks for the great job :)

#11 blueyes on 05.17.07 at 12:07 pm

I’m confused, so we copy what Simon has posted out of own related posts file into our template for it work without the php code or what?

#12 Alejandro Reyes Blog | Internet Marketing Tips for the Online Entrepreneur » Blog Archive » Blogging Tips: Day 5 - Keep your Readers Happy: Make it Easy for Them to Find Your Posts on 07.31.07 at 5:50 am

[...] them a list of related posts at the end of each blog post. You can accomplish this by using the modified Related Posts plugin from Peter Bowyer. He has enhanced the original plugin by Alexander Malov & Mike Lu [...]

#13 10 SEO Tips for WordPress at WordPress Themes, Plugins, Blog Tips, Make Money Online >> WPthemesplugin.com on 08.11.07 at 11:46 am

[...] Comment: You can find the plugins here, here and here [...]

#14 BlogSecurity » Top 11 WordPress Plugins on 09.16.07 at 12:11 am

[...] related_posts [...]

#15 WordPress Plugins Database » Plugin Details » Related Posts (modified) on 09.19.07 at 10:37 am

[...] Visit [...]

#16 Kr±g Starszoharcerski “Matrix” » Blog Archive » Usprawnienie nawigacji: podobne posty on 09.29.07 at 3:32 pm

[...] wtyczkê mo¿na pobraæ ze strony http://peter.mapledesign.co.uk/weblog/archives/wordpress-related-posts-plugin, skopiowaæ ten plik do katalogu wp-content/plugins, a nastêpnie w³±czyæ (wtyczki -> related posts [...]

#17 The Top 9 Wordpress Plugins To Help You Make Your Blog Stickier - Lost Art Of Blogging on 10.18.07 at 8:42 pm

[...] If you’d want a more advanced and more accurate Related Entries plugin you can use the hacked version by Peter Bowyer, that has messed around with the original code and made it take in consideration tags as well, so [...]

#18 gropius · Atualizações etc. on 10.20.07 at 3:22 am

[...] Atualizei o plugin que gerava uma lista de posts relacionados ao final de cada post. O plugin antigo gerava uma lista [...]

#19 » 9 plug-in must have per Wordpress Geekissimo on 10.22.07 at 10:00 am

[...] ogni post e, se non si è soddisfatti del suo operato, si può ricorrere alla versione di Peter Bowyer che dovrebbe ridurre i margini di errore nella correlazione tra vari [...]

#20 9 plug-in indispensabili per WP « autisticoBlog on 10.23.07 at 10:05 am

[...] sotto ogni post e, se non si è soddisfatti del suo operato, si può ricorrere alla versione di Peter Bowyer che dovrebbe ridurre i margini di errore nella correlazione tra vari [...]

#21 Open Mind » Blog-Archiv » Verweise auf verwandte Artikel mit WordPress on 11.03.07 at 7:54 pm

[...] oben beschriebene Similar Posts basierte ursprünglich darauf. Von Peter Bowyer gibt es zudem eine modifizierte Version.  [...]

#22 Optimizacion para Wordpress I on 12.19.07 at 1:50 pm

[...] instala el plugin para mostrar Entradas Relacionadas, este que recomiendo es una modificación del original para trabajar mejor con sitios en español. De todas formas la implementación del mismo puede [...]

#23 mitcho.com > blog > Yet Another Related Posts Plugin on 12.29.07 at 1:49 pm

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

#24 Burak Demir on 01.09.08 at 10:31 pm

I see the my comments show wrong.

Correction:

Here is
Hi. I’m glad to you publish this plugin..
But, I want to the related post plugin don’t show the after more tags of posts.
How can I do this?

#25 Shanx on 02.05.08 at 4:20 am

Hi Peter. Thanks for this absolutely excellent plugin!

My questions:

1. Does this work with WP 2.3 (before I screw up my production sites)?

2. Has anyone tried this with the “Similar Posts” plugin and compared the performance or accuracy? http://rmarsh.com/plugins/similar-posts/

Thanks!

#26 Shanx on 02.08.08 at 4:27 am

I think this is a good effort but the “Similar Posts” plugin is a godsend. It just works, the options are clear and concise, and the code is squeaky clean. For anyone who’s grappling with this plugin (as I was).

#27 Wordpress Related Post Plugin | Wassup on 02.20.08 at 3:30 am

[...] 2.0.4. Updated at 11:26 am on 20 Feb 2008 Just found an “improved version” of the Related Entries plugin by Peter Bowyer. Just overwrite the exisitng relate-posts.php with the one you download from Peter’s [...]

#28 Related Post Hack for Wordpress MU | Wassup on 02.20.08 at 6:09 am

[...] have done it for this blog. I used the Peter Bowler’s version of Related Post. It works. See [...]

#29 Wally on 02.20.08 at 8:38 pm

Hmmm. I tried to “override” the default (I have set up in the options page) of 5 items to 3 items using the parameters. Didn’t seem to work. I still get 5 items even though I set the parm to $limit=3. I’m not sure what I’m doing wrong.

#30 wehuberconsultingllc.com » Blog Archive » A quick review of two Related Posts plugins for WordPress on 02.25.08 at 2:00 am

[...] uses a different technique. He gratefully acknowledges the work of several people, Peter Bowyer, Alexander Malov, and Mike Lu. The key part of this plugin is the algorithm to identify related [...]

#31 Jak założyć dobry samogenerujący sie blog? | e-Biznes i Programy Partnerskie on 02.25.08 at 10:47 am

[...] Related Posts Plugin - wtyczka ta poprawi nam wewnÄ™trzne linkowanie i dodatkowo zróżnicuje treść. Wtyczka wymaga trochÄ™ wiÄ™cej umiejÄ™tnoÅ›ci od nas, bÄ™dziemy musieli wyedytować skórkÄ™ wordpressa, prawdopodobnie bÄ™dzie to plik single.php i po kodzie <?php the_content(’Czytaj dalej &raquo;’); ?>, wstawić <?php related_posts(5, 10, ‘<li>’, ‘</li>’, ”, ”, false, false); ?>. Do poprawnego dziaÅ‚ania należy także wykonać zapytanie sql do bazy danych: ALTER TABLE `wp_posts` ADD FULLTEXT `post_related` ( `post_name` , `post_content` ) Jeżeli nadal bÄ™dziecie mieli trudnoÅ›ci z instalacja wtyczki, przeczytajcie instrukcjÄ™ dostarczona razem z wtyczkÄ…, instrukcja jest po angielsku, ale nawet osobom nie znajÄ…cym jÄ™zyka powinna wydać siÄ™ zrozumiaÅ‚a. [...]

#32 Best (must have) Wordpress Plugins | Nihar's World on 03.17.08 at 11:27 am

[...] Related Posts - This plugin is a very useful plugin. It shows similar posts already made on your blog, regardless of their category thereby makes readers read other posts that are related to the current post. It is also one of the SEO (Search Engine Optimization) plugin because, it increases internal linking on your blog. After few days i found an “improved version” of Related Entries plugin by Peter Bowyer. [...]

#33 WordPress Plugins: 25 Best/Must Have WordPress Plugins || WordPress Themes, Plugins, etc.. on 04.11.08 at 6:12 am

[...] Download Link: http://wasabi.pbwiki.com/Related%20Entries Modified versions of the same plugin: http://peter.mapledesign.co.uk/weblog/archives/wordpress-related-posts -plugin [...]

#34 Spa… bloguj automatycznie | Szelo on 04.29.08 at 11:17 am

[...] Related Posts Plugin - wtyczka ta poprawi nam wewnętrzne linkowanie i dodatkowo zróżnicuje treść. Pod każdym wpisem będzie wyświetlała się wybrana ilość linków do innych wpisów. Jeśli chodzi o linkowanie to super sprawa. Umieszczając ten plugin trzeba zmodyfikować nieznacznie kod skórki Wordpressa i dodać zapytanie do bazy, nie będę tu opisywał szczegółów, bo wszystko opisano w instrukcji. [...]

#35 Gabfire web design » Wordpress plugins: 4 Jan 2008 on 05.09.08 at 8:34 am

[...] Another Related Posts Plugin Yet Another Related Posts Plugin is the result of some tinkering with John Bowyer’s version of Alexander Malov & Mike Lu’s Related Entries plugin. Modifications made [...]

Leave a Comment