Showing Related Posts Easily

Most blogs rely heavily upon chronology for browsing. While this preserves the diary-like origins of the blog, it's suboptimal for a person that happens upon your blog from a search engine referral. In that case, they often have no desire to spend any more time browsing your site. You need to make it easy for them to see the things they may enjoy.

It's often helpful to show some related entries or posts in the context of whatever they're currently viewing.

A few hacks exist for doing this in WordPress, and the best one is probably this plugin.

I'm not using WordPress, so I needed to implement this on my own. Luckily, it really only takes a few lines of PHP to implement a basic related entries plugin.

Let's assume a basic table structure for your blog, with title and body fields for storing your posts' titles and bodies. First add a MySQL FULLTEXT index to those fields so that they can be searched.

FULLTEXT (title,body);

Now, on your permalink page let's say you want to list some related entries after the body of the post. In my implementation, I use the title of the current post and search for other entries with any of those words in their title (excluding, of course, the current post which is clearly 100% related to itself).

$rposts = mysql_query("SELECT ID,title FROM 
(title) AGAINST ('$related' IN BOOLEAN MODE) 
AND ID<>$postid");

It would be easy to enhance this method. You could match the title and body, or if you had tags or categories you could factor those in to get more relevant results. Scroll to the bottom of this post to see it in action.