There are times when you will have custom post types that are related, and it feels natural to link them together in the database. For example:
- 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.
Normally, WordPress doesn’t let you connect different custom post types. You can easily create a hierarchy within a given type, but you can’t tell WordPress that an item of one type is a child of another item of a different type.
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 Content Templates.
How it Works
The best way to see how it works is to look at an example. Let’s take a look at two.
A Real Estate Website
Let’s say that we have a site with property post types and rooms post types:
- A property has rooms
- Rooms belong to a property
In this case, we will say that Property type is the parent of the Rooms type.
One of our properties has five child rooms.
You can see how this works in this diagram. We’ve made it even more complex by adding furniture to each of the rooms. Each piece of furniture can be its own post type as well.
In your database, each room points to its property parent. When we want to load all the rooms for a property, we query rooms that have a
_wpcf_belongs_property_id value equaling the ID of the property.
Types automatically manages the
_wpcf_belongs_PARENT_id fields of children and Views knows how to filter by them when you need the content.
When you edit a house, you’ll see the rooms that it has. From within the house edit page, you can add rooms and edit them.
Important: these rooms are not just an array of post fields that belong to the property. Instead, each is a Room post type, using its own fields. Types lists the child items in the parent editor, to make it easier to manage children. You can add child items right from within the parent editor, edit them and save.
A Car Rental Website
A car rental website would work a bit differently. You may need three different post types:
- Car – this would let you list each car in your fleet
- People – this post type would contain information about your renters
- Agreement – this will record every car rental by a person. Using the agreements information, you’ll be able to tell how many times a person rented a car (and which car) and how many times a car was rented (and by who).
A post type can have different parents of two different types. In this example, an agreement belongs to both a car and a person.
When a person rents a car, you will add an agreement item, which will be the child of that person and that car. The agreement will have additional information (post fields), such as the rate, date and other information specific to each rental.
When you want to list all the rentals of a person, you’ll create a View for agreements, filtered by a person parent. This will show all the rentals a certain person has had. Each agreement links to a car, so you can see which car was used.
Alternatively, you can load rentals by car. You’ll create a View that loads agreements for a car parent. Each of these agreements will also link to the person who rented the car.
Similarly to the house and rooms example, when you edit a person, you’ll see the agreements he or she has. Since the Car is another parent of Agreement, you’ll also see agreements when you view the a Car, but these will be the agreements that belong to that car, rather than by person.
Creating a Post Relationship
It’s simple to set up a post hierarchy. When you create a new custom post type you will see an option at the bottom of the page called Post Relationships (Parent / Child).
You can set a content type to belong to another content type. This allows you to create a highly relational website using custom post types.
Select Child Fields and Taxonomies to Display in the Fields Table
Once you have connected a parent to a child and saved your new custom post type, you can then decide which data you wish to show on the post editing screen for the parent.
Return to the custom post type admin screen of the parent, scroll down to post relationships section and click on Select fields.
You can then decide which fields should appear on the node’s editing screen. The options are:
- Title, all custom fields and parents – this will display all post fields attached to the node
- Do not show management options for this post type – this will not display anything
- All fields, including the standard post fields – this will display all of your post fields along with the default WordPress fields
- Specific fields – this will display only the fields and Taxonomies that you choose. This can be useful if you only want specific fields to be edited from the hub post editing screen, or if your node is connected to more than one hub
This method of creating content relationships makes it easy for you to connect multiple post types, and then go on to display them using Views.
More Reading on this Topic
Learn how to edit child posts from within the parent post editor:
Learn how to load child posts from the database and display them:
If you are using only Types plugin, to display parent or child posts on the front-end, you will need to edit your theme’s PHP templates manually. You can learn more about this on the following two pages: