WordPress Plugin: Custom post relationships (CPR) v1.01

June 10th, 2010 - Backend, Wordpress

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)

Comments

  1. Vassilis Mastorostergios

    June 10, 2010

    Oh boy oh boy lemme get my hands on this :)

  2. Apostolos

    June 10, 2010

    That’s rocking hard! Good job!

  3. Greg

    June 10, 2010

    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?

  4. Gerasimos

    June 10, 2010

    Hi Greg,

    I am not sure i’m following your example. Maybe something more specific?

  5. HitMan

    June 10, 2010

    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.

  6. Vassilis Mastorostergios

    June 11, 2010

    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.

  7. istavrak

    June 11, 2010

    great plug-in!!! Realy useful! I’ll check it out as soon as possible.

  8. klou

    June 11, 2010

    Great work!

    Keep up!

  9. Greg

    June 11, 2010

    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!

  10. finid

    June 14, 2010

    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.

  11. Amy Jones

    June 15, 2010

    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!

  12. Alarm

    June 15, 2010

    Finally, we could find a plug for my blog useful. Thank you for sharing my friend.

  13. marco.ragogna

    June 19, 2010

    Your plug-in seems what I was searching for. I definitely will try in the next weeks. Thank you for sharing it

  14. shawn

    June 20, 2010

    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

  15. Chad

    June 20, 2010

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

  16. Bonzo

    June 23, 2010

    Hi!
    Its great!
    But can you tell more clear about thumbnails code? Where it must be inserted?
    Thanks!

  17. Andres

    June 25, 2010

    Nice Solution:

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

  18. trichins

    July 19, 2010

    This plugin works great! Any pointers for how to show the Related Posts links in a sidebar widget?

  19. harryplusk

    July 23, 2010

    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.

  20. Gerasimos

    July 23, 2010

    To all: a new version with some really great features is coming really soon. Stay tuned!

  21. Brad Kelly

    August 5, 2010

    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?

  22. alimh

    August 19, 2010

    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.

  23. Hans

    August 19, 2010

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

    Once again thanks for sharing this plugin.

  24. Gerasimos

    August 19, 2010

    At the moment i am working on my WordPress themes but yeah, i’ ll be releasing a new version soon :)

  25. alimh

    August 23, 2010

    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.

  26. alimh

    August 27, 2010

    hey mate, i add post type ability to your plugins , if you like i can mail it for you.

  27. Hans

    August 30, 2010

    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

  28. Bone

    September 2, 2010

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

  29. Chris Burbridge

    September 13, 2010

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

  30. Ethan

    September 15, 2010

    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

  31. CaeerOutlook

    October 31, 2010

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

  32. 209141

    November 15, 2010

    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!

  33. thula

    February 9, 2011

    this is amazing, thanks!!

  34. Ryan Imel

    February 14, 2011

    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?

  35. Gerasimos

    February 14, 2011

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

  36. Ryan Imel

    February 14, 2011

    Yes, exactly. Displaying the reverse relationship is exactly what I’d like to do.

  37. Richard

    April 5, 2011

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

    Any plans ??

  38. Wes

    May 11, 2011

    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

  39. Tony

    May 30, 2011

    Great work, thank you Gerasimos

  40. geroge

    August 24, 2011

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

  41. idris

    October 22, 2011

    very useful and was looking for a solution, finally found it..thank you for sharing

  42. pey

    February 16, 2012

    hello your plugin is great
    i want the relationship will delete when i delete/draft the post that related.
    how can i do it ??

  1. Τα πρώτα μου Wordpress plugins είναι πλέον διαθέσιμα « theportraitofageek.com

    [...] μιλήσει και πλέον μαζί με κάποιες βελτιώσεις είναι διαθέσιμο για χρήση. Το 2o plugin (Custom CSS) προέκυψε από ανάγκη σήμερα το πρωί. [...]

    June 11, 2010

  2. WordPress Plugin Releases for 06/18 « Weblog Tools Collection

    [...] Custom Post Relationships (CPR) [...]

    June 18, 2010

  3. WordPress Plugins by Tsiger WordCamp Greece

    [...] “hardcore” λύσεις τις οποίες έπρεπε να ακολουθήσω. Custom CSS για posts και για pages! Εάν θέλετε να δημιουργείται διαφορετικό design για [...]

    June 20, 2010

  4. WordPress Wednesday: Custom Post Relationships « BlueFur.com

    [...] a different take on the “related post” function is the Custom Post Relationships WordPress plugin. This allows for the manual selection of related posts, ensuring that you are [...]

    July 21, 2010

  5. Wordpress Plugin: Custom Post Types Relationships | webtoolkit4.me

    [...] while ago we released CPR (Custom Post Relationships), a plugin that lets you create manual relationships among your posts. Problem was that custom post [...]

    October 9, 2010

  6. WordPress Plugin: Custom Post Types Relationships | Web Design Northamptonshire

    [...] WordPress Plugin: Custom post relationships (CPR) v1.01 [...]

    October 10, 2010

Leave a Reply