Resources for Web Workers

From the blog

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>
$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>";

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!


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


  1. 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?

  2. 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.

  3. 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!

  4. 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.

  5. 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!

  6. 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

  7. @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!

  8. Nice Solution:

    How associate to Related Post a Image via custom fields
    or current_theme_supports (post-thumbnails), comments, tags etc.

  9. 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?

  10. 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?

  11. 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.

  12. 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.

    $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 “&nbsp”;

    Once again thanks for sharing this plugin.

  13. 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.

  14. 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. :-)



  15. 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! :)

  16. 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.)

  17. 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 /[at]/ shawrepublic com

  18. 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!

  19. 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?

  20. @Ryan: Well at the moment that needs some extra coding to achive that :) You mean something like display the reverse relationship, right?

  21. You said back in june 2010 about Future versions
    that may include custom content types and page relationships.

    Any plans ??

  22. 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


  23. I have couple of user roles setup and would like to restrict the CPR plugin function only to the administrator. Is this possible?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>