WordPress Plugin: Custom post relationships (CPR) v1.01
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)

Vassilis Mastorostergios - June 10, 2010 8:50 pm
Oh boy oh boy lemme get my hands on this :)
Apostolos - June 10, 2010 9:23 pm
That’s rocking hard! Good job!
Greg - June 10, 2010 10:53 pm
I don’t mean this as a criticism, but only as a serious enquiry: what does the plugin do that couldn’t be accomplished using appropriate tags? So in the above screenshot, they would be tagged with “contests” and then you could do whatever you want with posts that have the “contests” tag?
Is it just a matter of saving the time that would be spent manually updating all those posts to contain a “contests” tag?
Gerasimos - June 10, 2010 11:02 pm
Hi Greg,
I am not sure i’m following your example. Maybe something more specific?
HitMan - June 10, 2010 11:44 pm
Great to see this gem released, awesome work m8!
@Greg: the thing you’re describing is “auto” relation match via tagging. That’s totally different.
This is a manual relationship *builder* that doesn’t exist in WP. It’s really useful, especially with the filter/search bar Gerasimos has implemented.
Vassilis Mastorostergios - June 11, 2010 12:02 am
Greg, lets you choose and bind two posts or more together. So for example you can display and pull anything you want from one post to another.
What you’re describing can’t be achieved in the same way with tags.
istavrak - June 11, 2010 8:42 am
great plug-in!!! Realy useful! I’ll check it out as soon as possible.
klou - June 11, 2010 11:21 am
Great work!
Keep up!
Greg - June 11, 2010 4:48 pm
Call me thick, but you can still do what you want to do with tags. It’s only “automatic” insofar as you create the behaviour of what to do with those tags.
But with a couple days to reflect, I can definitely see how the plugin makes the task a breeze rather than a slog. Nice work!
finid - June 14, 2010 11:22 pm
I got really excited until went against the short code idea. What would make this really stand out is to have short code option to add related posts from a category or category range into post pages.
Amy Jones - June 15, 2010 3:15 pm
Very nice, I’m a huge fan of WordPress and it’s good to see new quality plugins getting released. I’ll download and check it out, looks exactly like what I have been looking for for quite a time. So, thanks alot!
Alarm - June 15, 2010 8:13 pm
Finally, we could find a plug for my blog useful. Thank you for sharing my friend.
marco.ragogna - June 19, 2010 2:53 pm
Your plug-in seems what I was searching for. I definitely will try in the next weeks. Thank you for sharing it
shawn - June 20, 2010 2:05 am
Nice addition.
I am already using 3.0 with multiple custom post-types including multi-relational post-types. Looking forward to the new vs. of your plugin that supports post-types
Chad - June 20, 2010 5:45 pm
@Greg, Your Monkey Gravatar icon is very fitting for your profile.
This plugin gives more control over the related posts. The ones out there right now are automatic, and many a times, you want to exclude some posts that the automatic plugins include as related, or vice versa.
If you don’t understand this also, then get the iPhone Brainiac App with hopes of growing your monkey brain!
Bonzo - June 23, 2010 12:58 am
Hi!
Its great!
But can you tell more clear about thumbnails code? Where it must be inserted?
Thanks!
Andres - June 25, 2010 3:32 am
Nice Solution:
How associate to Related Post a Image via custom fields
or current_theme_supports (post-thumbnails), comments, tags etc.
trichins - July 19, 2010 10:25 pm
This plugin works great! Any pointers for how to show the Related Posts links in a sidebar widget?
harryplusk - July 23, 2010 12:02 am
Amazing plugin. I will totally use it. I would like to ask though if there’s any chance to show a little bit of the post’s content in related posts and not just the title. Is this possible?
Thnx.
Gerasimos - July 23, 2010 8:07 pm
To all: a new version with some really great features is coming really soon. Stay tuned!
Brad Kelly - August 5, 2010 7:31 am
Great work! Installs beautifully and functions great. I am planning to use this to show the relationships between posts about people and posts about particular groups they may belong to, similar to a directory.
In this sense, it would be great if the relationship was reciprocated. That is, if I edit Post A to say that Post B is related, it’d be nice for Post B to immediately identify Post A as related. Currently, I have to edit both posts to show the relationship between the two it seems.
I’m not sure I articulated that all that well, does it make sense?
alimh - August 19, 2010 8:00 pm
hey mate, your plugins is awesome and able to handle a part of my idea.
do you able to expand this plugins?
my idea is to let visitors to able Relate posts and send their reasons why these posts are related.
Hans - August 19, 2010 8:17 pm
Hi,
Just to thank you for this incredible plugin. It’s exactly what I was looking for.
I just would like to make to a remark on your text (above). There is a lot of people that use let the entire text on the main page so, let them know that add your snippet to the main page as well can be a good idea.
I also made a small change in the snippet so the tittle “Related posts will only show when there is a something.
<?php
$relations = cpr_populate(get_the_ID());
if (!empty($relations)) {
echo "Lire également:“;
}
if (!empty($relations)) {
echo “”;
foreach ($relations as $related) {
echo “ID).”‘>”.$related->post_title.”“;
}
echo “”;
}
else {
}
echo “ ”;
?>
Once again thanks for sharing this plugin.
Gerasimos - August 19, 2010 9:17 pm
At the moment i am working on my WordPress themes but yeah, i’ ll be releasing a new version soon :)
alimh - August 23, 2010 3:04 pm
i hope you update it sooner, i really need this plugins for my new site. it do what i want just add post type feature pls.
alimh - August 27, 2010 2:46 pm
hey mate, i add post type ability to your plugins , if you like i can mail it for you.
Hans - August 30, 2010 12:36 pm
Hi Gerasimos,
Your plugin made my life a lot easier specially when I have to find related posts. Great tool.
I just would like to know if it’s possible to add a search by Tags in a future version.
Searching by tags within a category or alone can reduce drastically the number of entries returned by the search engine.
Well, just an idea. :-)
Cheers
Hans
Bone - September 2, 2010 3:39 pm
Yours plugins is cool!
A council, if you had not already thought… works with the POST_TYPE (for the custom fields) and not with “post” (more limited)… and to create a system automatic rifle of bidirectional relation.
After these modifications… for me… you are the king of the plugin relational! :)
Chris Burbridge - September 13, 2010 9:22 pm
This is great thing. VERY nice interface! (important.)
All in 138 lines of code !?
What I would love to do is look at how to incorporate this to make relationships between two different custom post types. This would really take us much closer to a relational database functionality for WordPress. In many cases, this is all anyone would really need to turn WP into a relational database application.
I think all that would be required for this, is to be able to specify the post type under which the UI would be displayed, and the post type that would be pulled into the UI. (alimh, you mentioned something about this. Would like to see it.)
Ethan - September 15, 2010 2:24 am
Gerasimos,
We are working on a very complex WP site for a client and need a way to involve CPTs. We would be willing to buy a working CPT version of the CPR plugin from you if you are willing. :)
~Ethan
ethan /[at]/ shawrepublic com
CaeerOutlook - October 31, 2010 4:17 am
This is a great plugin, I alwasy wanted to show the related posts of my own choice and this plugin just allow me to do that.
Awesome….thanks :)
209141 - November 15, 2010 8:46 pm
Hi,
Hope this is not too elementary. I am on the Thesis layout. I don;t seem to have a ‘single.php’ file. Am I missing something? Or where do I fill in the extra code to display the related posts?
Thanks in advance for any extra help!
thula - February 9, 2011 3:56 pm
this is amazing, thanks!!
Ryan Imel - February 14, 2011 3:54 pm
Great work, this plugin is very useful.
Curious, what if I wanted to query and list exactly the opposite? In other words, what if I had a post, and then wanted to list all of the posts that listed that post as a “related” post. How would I do that?
Gerasimos - February 14, 2011 4:18 pm
@Ryan: Well at the moment that needs some extra coding to achive that :) You mean something like display the reverse relationship, right?
Ryan Imel - February 14, 2011 5:20 pm
Yes, exactly. Displaying the reverse relationship is exactly what I’d like to do.
Richard - April 5, 2011 7:58 pm
You said back in june 2010 about Future versions
that may include custom content types and page relationships.
Any plans ??
Gerasimos - April 6, 2011 11:48 am
http://www.cssigniter.com/ignite/custom-post-types-relationships/ :)
Wes - May 11, 2011 12:42 pm
Brilliant plugin, absolute must have! Just installed now was wondering what exact code i need to add to get the thumbnails to show and where? Newbie! :D
Thanks,
Wes
Tony - May 30, 2011 4:45 pm
Great work, thank you Gerasimos
geroge - August 24, 2011 3:42 pm
I have couple of user roles setup and would like to restrict the CPR plugin function only to the administrator. Is this possible?
idris - October 22, 2011 7:12 pm
very useful and was looking for a solution, finally found it..thank you for sharing
pey - February 16, 2012 6:08 pm
hello your plugin is great
i want the relationship will delete when i delete/draft the post that related.
how can i do it ??