Post relationships allow you to create connections between your custom post types. Using them means you can eliminate data duplication, meaning each element only needs to be edited once.
After you create your post relationships, you will need to add useful content to your custom post types. One of the easiest ways to do this is by using repeatable field groups. Repeatable field groups let you add a set of fields to a custom post type as many times as you need without having to recreate them one by one.
One of the best ways to implement both of these features is by using Toolset’s suite of plugins (WP Mayor readers can get an exclusive 20% discount when they purchase Toolset). Toolset allows you to create relationships between post types without using any PHP at all – saving developers hours of unnecessary work.
Within this post we will explore the following:
- The different types of post relationships and how they benefit you
- How to create a many-to-many relationship with Toolset
- Creating connections between custom post types on the front-end
- Using repeatable field groups to easily add content to your custom post types
When you would use the three types of post relationships
- One-to-one relationship
The one-to-one relationship is an exclusive connection between a child and parent element of different custom post types. Both the child and parent will only be linked with one element from the other custom post type.
For instance, on a travel website selling guided tours, you might want to create a connection between your “tours” and “special offers” custom post types. Let’s imagine that you are advertising a tour of The Louvre in Paris. You could very well offer a 20% discount at the gift shop inside. Now obviously that 20% discount won’t make sense for your other tours which do not go inside The Louvre, so they would need an exclusive connection.
The advantage of connecting these two custom post types is that you only need to edit either of them once and the changes will automatically appear on the other custom post. For example, if you wanted to increase the gift shop discount to 30% you would not have to do it twice – once on the special offer post type and also on the Louvre tour post type itself.
- One-to-many relationship
One-to-many relationships occur when a child post can only be linked with one parent but the parent can be connected to many children.
Imagine that you have several tour guides for your tour of The Louvre and you want to assign their shifts on the front end so tourists know who is the best person to contact.
First, we will create two custom post types – one for “tour guides” and a calendar of the “tour schedule.”
Then, all of our tour guides will need to be connected with the tour schedule, but each date will only need to be linked to one tour guide. For example, the 30 May tour will be taken only by Maria. However, Maria will also be taking the 2 June, 5 June, and 8 June tours as well. Therefore, we would use a one-to-many relationship to link the one tour guide (Maria) with multiple tour dates.
- Many-to-many relationship
Many-to-many relationships are used for elements from two custom post types which will need to be connected with multiple elements from the other custom post type.
A tour company in Paris might run multiple tours and employ a team of tour guides who share the tours between them. Tour guides might be tasked with giving many different types of tours while there could be multiple tour guides on each type of tour.
Instead of manually adding each tour guide to a tour, we can create a many-to-many relationship between the two custom post types. This saves developers a significant amount of time when it comes to building and running a website. This way, you do not need to duplicate content and risk creating unforeseen complications. Let’s take a look at exactly how you can create this many-to-many relationship.
Creating a many-to-many relationship using Toolset
To create our many-to-many relationship between our tour and tour guide custom post types we will use Toolset’s relationship wizard.
From your site’s admin, go to Toolset → Relationships and add a new relationship. In the wizard, select the “Many-to-many” option.
On the next page, we can decide which custom post types we want to connect with each other.
We can now choose to limit the number of post types we will link with each other. For example, we might want to limit the workload of our tour guides by only assigning them to three tours, and we may only need three people to cover each tour.
In the next section, we can add specific relationship fields, but this is unnecessary for the purpose of this many-to-many relationship. Therefore, we can move to the final slide and name it. We’ll call it “Tour relationships.”
Our “tour” and “tour guide” custom post types can now be connected with multiple elements from each other.
We can assign new tours to our tour guides on the back end in two ways. The first is to use the “Add new tour” option to create brand-new tours. Or, we can click on “Connect existing tour” to connect our tour guide with tours that already exist.
Let’s use the second option to attach the tour guide, Ella, to the Paris & Food tour. As you can see below, when we click on the option we can select the tour on the dropdown list.
We can now see on the front end that Ella is one of the guides for the Paris & Food tour.
Not only can we assign tours to our tour guides on the back end, we can also easily create a form to make it possible on the front end as well.
Using front-end forms to connect your posts
Many-to-many relationships and other types of post relationships can be created on the front end in just a couple of clicks thanks to Toolset’s Relationship Forms feature.
To create a form, we need to be sure to have the Toolset Forms plugin installed and activated on our site. Then, we go to Toolset → Relationship Forms to create a new form.
We can insert this form on any page or post. We will add a Toolset Form block, and choose the relationship form we just created. In the right sidebar, we can choose how the form works. Then, publish the page.
Once we have added the necessary fields our form is ready for the front end. Now if we wanted to assign Paula to the Paris By Night tour, for instance, it is simple to do without having to log in to the back end.
Once we click submit, we can now head to Paula’s tour guide page to see that it has been updated with her new tour.
From Paula’s post, we are able to click on the tours she is giving and find out more about each of them. However, we have yet to add relevant information to our “tour” custom post types for users to browse and read up on. One of the most efficient ways to do this is by using repeatable field groups.
How repeatable field groups help you add content efficiently
We can use repeatable field groups to display a list of the same type of information on a post, page, or custom post type. Developers can use repeatable field groups to make them once and reuse them as much as they like. This saves them a significant amount of time from individually creating a set of the same fields over and over again,
When a customer clicks on the tour guide post type they will expect to find out certain information about the tour. For example, they might want to see a slideshow of images to discover what exactly they will be visiting. Instead of adding the image, title, and caption fields each time, we can create a group of them once to reuse thanks to repeatable fields.
As you can see below, we now have a slideshow of photos from the Eiffel Tower tour for people to view.
For developers and content managers with a lot of information to manage, utilizing both post relationships and repeatable field groups make it easier to build and run your advanced site.
Download Toolset today to find out how easily and quickly you can build advanced websites. We’ve teamed up with the team behind Toolset, to offer you an exclusive 20% discount.