Related — A simple related posts plugin for WordPress
Over the last couple of weeks I’ve been busy writing a few custom extensions for WordPress to enhance the functionality of my websites. I want to release a few to the public as others might find them useful too.
The first one is a very simple plugin for adding a list of related posts to a blog post. Showing related posts can significantly increase the page views per visit as it can be used to highlight posts of the same topic that the user might be interested in as well.
There are already quite a lot of ‘related posts’ plugins out there. Many of them come with a lot of clutter and often the list of related posts is generated automatically based on weird algorithms. This may be useful for large blogs, however for my own ones I simply wanted to have an option that lets me choose the posts and their order myself when I write or edit a post.

Features
- Add related posts to your blog posts, pages etc.
- Choose from posts, pages or any other post type
- Support for custom post types
- Re-order related posts via drag and drop
- Custom markup possible, or simply use the default output
Download & Installation
Related is hosted in the WordPress.org plugin directory. You can use the plugin manager in WordPress to install it.
To install it manually simply extract the folder with all files, move it into your wp-content/plugins/ directory and activate the plugin from the WordPress directory.
Usage
To display the related posts, you need to add a small snippet of code to your WordPress template. You have the option of either displaying the default output – which is a simple <ul> list – or you can have the plugin return an array with all post data for custom markup and styling.
Example 1 – Default output
<?php echo $related->show(get_the_ID()); ?>
All you need to do is passing a post ID as argument. The above statement can be used inside the WordPress loop.
Example 2 – Custom markup
<?php
$rel = $related->show(get_the_ID(), true);
// Display the title of each related post
foreach ($rel as $r) :
echo $r->post_title . '<br />';
// To display all options use: var_dump($r);
endforeach;
?>
To return an array, simply pass true as second argument. It will return an array of post objects. Use var_dump() to see all the options.
Update October 2010
The code is now on GitHub, feel free to fork it and submit back your changes.
Update September 2011
Related has received a major update that incorporates a lot of things people have requested, like support for custom post types. Be sure to update to version 1.1.
Comments
Grrrreat! I think this might be the best Related Posts plugin for WP. I’m using now the Yet another bla bla, but I’ll test out yours too now! Great work!
Ah, perhaps an alternative for Microkid’s Related Posts, which also lets you you choose which one to use. I’d like if this one had a shortcode, but this is also managable of course.
Agreed, the one you mentioned looks like a good alternative with more options, I might have used it myself if I had known it before. Oh well…
This one is actually better suited for my needs. This one can be sorted alphabetically (with the drag & drop).
Just a small tweak to this beatiful piece of code. in the args passed in the wp_query in line 145 you should add ‘post__not_in’ => array($post_ID) in order to exclude the current post from showing in the select as there’s no reason (quick thought) to relate a post with itself :)
Good idea, I will change that in the next version.
Is there a way to get this to show up in custom post type edit screens? How would you go about limiting the list of posts that you can select by post type?
Thanks – it looks like a really nice plugin.
Will
Right now you would have to edit the source code – the
$queryarray – to change the types of posts it displays. I just haven’t had time yet to integrate a couple of improvements. Maybe a future release will come with config options, I’ll make a note for adding post type selection.ok, thanks for the directions.
I changed the query to
$query = array(
'posts_per_page' => -1,
'what_to_show' => 'variants',
'nopaging' => true,
'post_type' => 'variants',
'post_status' => 'publish'
);
(i don’t know what “what_to_show” does?)
and added in a line to show the related selector in screens for post types other than ‘post’
// Adds a meta box for related posts to the posts screen
//add_meta_box('post-meta-boxes', 'Related posts', array(&$this, 'displayMetaBox'), 'post', 'normal', 'high');
// Adds a meta box for related posts to the products screen
add_meta_box('post-meta-boxes', 'Related posts', array(&$this, 'displayMetaBox'), 'products', 'normal', 'high');
This is all in related.php file in the plugins folder – should anyone else read this.
Thought id report back what hacks i’d applied. I havn’t made a plugin so im not sure where i’d start if i was going to add a settings page myself?
Thanks again.
Hi Will, thanks for sharing. If you make changes to the plugin code, you only need to make sure that you don’t accidentally upgrade to a future version (in case I release one), because this would overwrite your changes.
I too have made changes so that the plugin can use all custom post types. I was going to submit the code back to you in the next couple of weeks, in case you wanted to include it.
Right now, I wish the plugin made bi-directional relationships. If I relate on one post, I would like to go to the other post edit screen and see the same relationship. I am going to make it do this, and submit back to you, in case you want it.
Unless you have plans for this?
M
Currently motivation for extending it is a bit low since I don’t use the plugin on any of my own projects anymore. But I can see there’s demand, so if anyone wants to extend it, I’m happy to review the changes and release a new version then, with credits. I had actually started with implementing bidirectional linking some time ago, using checkboxes to enable/disable it per link, but I didn’t finish it as far as I remember.