Saltar navegación

Many-to-Many Post Relationships in WordPress

Many-to-many relationship allows you to connect a post of type A to many posts of type B and vice versa. For example, an "event" has multiple "speakers" and a "speaker" can appear in multiple "events".

Setting Up Many-to-Many Post Relationships

To set-up many-to-many relationship between posts, go to ToolsetRelationships. Choose the Many-to-many option and follow the wizard to choose the posts to connect.

Setting-up a many-to-many relationship

One of the wizard step is to add fields to the relationship. This option is only available for many-to-many relationships.

Adding relationship fields

Let’s look at a few examples to see what this does.

ApplicationConnectingRelationship fields
Event websiteEvents <> SpeakersLecture title, time, location, description
RecipeDish <> IngredientOrder, quantity
Music albumsAlbums <> SongsTrack number
Relationship fields in many-to-many relationships

When you add «relationship fields», you are creating what we call an «intermediary item» for the relationship. This intermediary will hold the custom fields of the relationship. You can display these fields or use them as conditions.

After creating a relationship between two kinds of posts, you will see a box at the bottom of the post editor. It allows you to connect between posts.

Connecting related posts

From here, you can connect existing posts or create new ones that will immediately appear connected.

With Toolset, it is very easy to display related posts.

The most common way to display related posts is when editing a Content Template of a type that’s in a relationship. For example, when we’re designing the template of an «event», which is related to «speakers».

Since it’s a many-to-many relationship, almost always, you will display related posts using a View. A View lets you load content from the database and display it as a list, with any styling that you choose.

All you have to do is insert the View block and in the creation wizard, select which related content it should display.

Creating a View for related items (speakers related to an event)

Now, proceed to design the View.

In this View, you can also display fields that belong to the relationship. For example, when we list the speakers, we often want to show the titles of their lectures. To display fields that belong to the lecture (the relationship), insert any Toolset block that you want and enable Dynamic Source. Then, in the Post source, select the relationship. Now, you can choose fields that belong to the relationship.

How to select a field of the relationship