Home » Course lessons » Using Post Reference Field to Set-Up One-to-Many Relationships in WordPress
Using Post Reference Field to Set-Up One-to-Many Relationships in WordPress
The Post Reference field allows to link from one post to another. Under the hood, this field creates a one-to-many relationship, which allows you to display the related posts.
The Post Reference field allows you to connect only one post of one type to one post of the other type. This means, you’re pointing from the “many” side to the “one” side.
For example, you might connect an “room” post to a “house” post (each room can belong to one house). When you display a room, you can also easily display fields that belong to the house.
Steps for Adding a Post Reference Field
Go to the Toolset → Custom Fields page and create a group of fields, or edit an existing one.
Click on the Add New Field button and select the Post Reference field. You can only add a post reference field to a group assigned to one particular post type.
Select a type of post that the post reference field will connect to.
When you now edit any post of the selected type, there will be a box allowing you to connect it to any post of that related type.
Displaying Fields that Belong to the Related Post
You can use the Post Reference field to display related post’s fields anywhere you need to. This could be a template for single posts or an archive page.
Edit a Content Template or an archive page.
Use any Toolset block you want to display fields coming from the related posts.
In the right sidebar, select to use a dynamic source and in the Post Source dropdown, select the Parent: <post type name> option. Post type name is the name of the post type you connected using the Post Reference field.
Finally, select the exact field you want to display.
Repeating Post Reference Fields
Sometimes you might want to connect one post to multiple posts by making your Post Reference field into a repeating one.
However, a repeating Post Reference field implies creating a many-to-many relationship. For example, an event could be connected to many announcements and single announcement could be connected to many events.
This is why Toolset doesn’t allow you to make the post reference field directly repeatable. Instead, it offers to automatically turn your post reference field into a post relationship.
To do this, edit the field group containing your Post Reference field, edit that field, and click the I need this field to repeat link.
A pop-up dialog appears, suggesting you need to use a many-to-many relationship. Click on the Preview change button to get an overview of the necessary changes in the relationship.
If everything looks good, click the Make this change button. Toolset will automatically create the necessary post relationship and remove the Post Reference field from the group.