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 cars. Rental 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
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
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.
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.
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.
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.