Update: Plugin is now hosted in the official Plugin directory: http://wordpress.org/extend/plugins/custom-post-relationships/

Hello everyone. It’s been a while since the last post but i think it’s worth it. It’s a wordpress plugin and it’s all about custom post relationships. All these years i was looking for a plug-in that would let me relate posts manually. None existed. All of the good ones rely on some short of algorithm and they pull a list of related post based on that.

In various projects all i need is a simple list of posts that i can relate to each other. And that’s what i did.

How it works

So first, download the plugin, extract and upload it to your plug-ins folder. Activate it and you are all set. If you create or edit a new post you will set a screen similar to the one above. You can choose how many posts you want to see, in what order, from what category to pull posts from and of course you can filter the results by typing a few letters from any part of the post title. Then all you have to do is add the posts to the Related Posted pane by clicking “Add”. In the related posts pane you have the ability to reorder the posts just by dragging and dropping.

And that’s it actually. Your post is now in a relationship with a series of posts that YOU chose. Can you imagine the possibilities?

How to display these posts

I have put a lot of thought on that. First i was thinking to create a shortcode that you could use in your posts but that approach was limited. So i decided to go with the “hand-coded” version which also provides a flexible way of displaying related posts. It’s very easy actually. If you have put your hands on wordpress templates file, you’ll pick this up in a minute. Here it goes.

Open your single.php file and locate the place that your want to display your related posts. Enter this piece of code:

<div id="related">
<h2>Related Entries</h2>
<?php
$relations = cpr_populate(get_the_ID());
if (!empty($relations)) {
echo "<ul>";
foreach ($relations as $related) {
echo "<li><a href='".get_permalink($related->ID)."'>".$related->post_title."</a></li>";
}
echo "</ul>";
}
else {
echo "<p>No related entries</p>";
}
?></div>

Presto! This small piece of code will create a new section (div id=”related”) with a section title (Related entries) and will display an unordered list with the related posts. If there aren’t any relationships set for a post a “No related entries” message will be shown. Have in mind that in the loop (foreach) you have access to every element of a post. For example if you want to display the thumbnail for a post (2.9.0) you could add the following to the code above:

get_the_post_thumbnail($related->ID, 'thumbnail')

This should go next to the post_title for example and display the post’s main thumbnail. Actually you can display anything. Even custom fields.

Current Version

Current version of Custom post relationships (CPR) works in WordPress 2.9.0+ including WordPress 3.0 RC1. For the time being it works only for posts. So no pages for now.

Future versions

I have already created a fork of this and i am working on a version just for WordPress 3.0 that includes custom content types and pages relationships. Expect though an options page for that as things are getting a bit more complicated for WordPress 3.0.

I hope you find this plug-in useful and i’d love to let me know how you use it. Also do not hesitate to share ideas and comments. Have fun!

Changelog

June 10th, 2010 : Initial release.
Junue 11th, 2010: “All categories” option added. (thank you: @christoschiotis)

Related posts:

  1. WordPress Plugin: Custom Post Types Relationships
  2. WordPress Custom Post Type Code Generator
  3. WordPress plugin: Custom CSS stylesheet for posts or pages
  4. WordPress plugin – Ultimate CMS
  5. WordPress plugin – P3 (Plugin Performance Profiler)