DOCS

Post relation field

The Post relation field allows you to create relationships between different post types.

General options

  • Field name
  • Field key
  • Placeholder
  • Description
  • Is Required
  • CSS Classes
  • Default value

General options

  • Relation type: There are four relation types Has one, Has many, Belongs to One, Belongs to many
  • Related to: Lets you pick one or multiple post types
  • Limit post selections by author: Lets you limit relationships between posts by the same author only, or remove the restriction so all posts on the site regardless of their author can be related.
  • Require approval for posts that belong to a different author: If there’s no restriction by author, you can allow relationships to be established right away, or to require approval by the author.
    If you choose to require approval, that means when you relate one of your posts to a post by someone else, they will receive a notification to Approve or Decline the relationship.
  • Enable non-published posts for selection: Enable ability to create relationships between non published posts – Draft, Pending, Unpublished, Rejeced
  • Use custom relation key: By default, the field key will be used as the relation key. Enable this setting to use a custom relation key instead.

Example

As an example, we have the following post types in our site

  • Places
  • Events

We can submit Places and Events in our site as separate post types, but we also want to associate different Places to different Events, for example when a Place that is listed on our site is hosting an Event, we want to establish that relationship.

To create this relationship, we can use the Post relation field.

Adding the field

We have edited the Places post type and added the Post relation field.

As we are going to select Events that are related, we name the field “Select Event”

Under Relation type, we selected Has many as we want Places to be able to host many Events

Under Related to we selected the Events post type

Finally, we added a custom relation key event-place as a unique identifier for this relationship.

Now that we have created the field, in the post submission form for Places post type we have the ability to select Events that are related

Two way synced relationship

So far, we can pick an Event during Place submission. If we want to also add the ability to pick a Place during Event submission, we can edit the Event post type and add a Post relation field on that post type aswell. (This is optional, not required for the relationship to work)

We’re gonna name this field “Select Place” instead

Relation type: We pick Belongs to one as we want each Event to be related only to one Place

Related to: We select the Places CPT

Custom relation key: We use the same key event-place as we did when configuring the field on the Places CPT.

This ensures the two post relation fields are synced with each other, so making changes to one affects the other and vice versa.

Continue reading