WordPress Plugins to Link and Relate Post Types

Custom Post Types are a great tool for us WordPress developers, but sometimes we need to create relationships between them, and this is still something which isn’t included in the core functionality of custom post types.

Take for example a real estate site where you have houses and rooms, both set as custom post types. It would be natural to want to relate them in some way. It would also be really handy if on the admin side we could edit a house and within the same page be able to modify rooms related to it.

Some other examples:

  • A car rental site – People rent carsRental agreements will link between the two and allow to query who rented which car.
  • A library site – there are many books by different authors. You can lookup books by authors or find the author of any book.
  • A real estate site – houses will have rooms. When you edit a house, you can add as many rooms as you need.

There are a number of solutions through which one can relate post types:

  • Custom Field (wp_postmeta table)
    Store the related post’s id in the postmeta table. Works well for one-to-many relationships.
    – Plugin: Types
  • Custom Table for Relationships
    Create a separate table for storing the relationship. The good thing is that you can store more information about the relationship apart from the ids themselves. This method is great for many-to-many relationships.
    – Plugin: Pods
    – Plugin: Posts 2 Posts
  • Parent/Child Relationship (post_parent column)
    WordPress does this in core, for example when storing a page’s revisions. BBPress uses this, Topics are a parent of replies. WooCommerce also uses it for storing variations of a product. So the product will be a parent of the variations. Again this can only store one-to-many relationships.

Since there are no straightforward ways to create relationships in the WordPress API, we are best off using a plugin. Here are the options:

Posts 2 Posts

This plugin allows you to create many-to-many relationships between posts of any type: post, page, custom etc. A few example use cases:

  • manually curated lists of related posts
  • post series
  • products connected to retailers
  • etc.

Additionally, you can create many-to-many relationships between posts and users. So, you could also implement:

  • favorite posts of users
  • multiple authors per post
  • etc.

Here’s a handy walkthrough on how to make Posts 2 Posts work. Pippin Williamson has also reviewed this plugin recently.

Get Posts 2 Posts

Types

Types lets you create relationships between different post types. This means that you can now easily make rooms children of a house. Then when you display the house, use a View to load and display the different rooms. And when you display a room, you can easily point back to the house.

Types makes all of this easy. You’ll use GUI controls to connect different post types to one another. Views will help with displaying that information, by including the parent and children information in Views and View Templates.

How does it work? Types keeps the parent of a post as a custom field called _wpcf_belongs_parent_id. The _parent_ bit gets replaced by the slug of the parent. You can later query these relations using the Views plugin. All this can be done using the UI.

Of course Posts 2 Posts has more functionality, it lets you define all sort of relationships, but you have to write code to define and query those relations. The power of Types is thus it’s use in combination with the Views plugin.

Click this link for more information about how Types helps you create post relationships.

Get Types

Subordinate Post Type Helpers

This is a very new plugin created as part of Randy Hoyt’s WordCamp SF talk.

It helps you create related post type based on the Parent-Child functionality which is in-built with hierarchical post types. It provides a number of helpers functions to aid in the creation of subordinate relationships between two post types, one-to-many relationships in which posts of one type are children of another. (For example, a Job Listing might have a Company as a parent; a Meeting might have a Group.) With this plugin activated, developers can easily write code in their plugins and themes to register two post types related in this manner.

To understand better how it works you can check out the video of Randy’s speech or his slides.

Get Subordinate Post Type Helpers

Pods Framework

Pods uses the concept of relationship columns when relating post types. A relationship column is essentially a select list containing items from the related type. If you create a column in one pod that relates to another pod, “event”, then in the edit form you’ll see a select list with all “event” items.

You can also create Bi-directional Relationships. Bi-directional Relationships, as the name suggests, allows you to make “double sided” relationships. With Bi-directional Relationships it’s possible assign a pick column to a pod and, for the pod picked, a “reverse pick” to that first pod.

For example, you’ve got 2 pods: “author” and “book”. To create a bi-directional Relationship, you’d add a “related_books” PICK column in your “author” pod, and a “related_author” PICK column in your “book” pod.

Then, let’s say that you add 10 new books. For each book, you select an author from the dropdown list. With a bi-directional relationship you won’t have to go back into the author pod and manually select the related books from the dropdown. So every time each item is updated, the relationships on both sides will be updated.

Get Pods Framework 2.0

Have you done anything similar? Share your experiences in the comments sections below!

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

About Jean Galea

Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Related Articles

  • How to Convert Post Types in WordPress

    Ever wondered if it's possible to convert from one custom post type to another? Yes it is! This can be very useful for example when switching themes. One theme can…

  • Custom Post Types and Custom Fields were the two features that really transformed WordPress from a simple blogging tool to a fully-fledged CMS. Many still think that custom post types…

  • A forum on your website can increase visitor engagement and allow you to start a loyal community. There are a few WordPress forum plugins available. Let's take a look at…

4 Responses

  1. Sidra
    Sidra October 4, 2012 at 17:52 | | Reply

    very well information here. I have read your blog it is very helpful for me.

  2. Rahe
    Rahe November 15, 2012 at 08:26 | | Reply

    There is “Relation post types” for this too 🙂

  3. Virtual Web Solutions
    Virtual Web Solutions December 26, 2012 at 18:20 | | Reply

    Thanks for sharing a very nice post on creating relationships between post types. I believe this article will definitely help me and other WordPress developers.

Leave a Reply