Relation field

Display a selection of posts in the order of your choice. Available with Modern Fields Pro.

Where WordPress only lets you display posts in chronological or alphabetical order, the relational field lets you select the posts to display, in the order of your choice.

This field is only available in the pro version of Modern Fields.

Configuration

First, go to Modern Fields > Fields groups and add the Relation Field block.

In the inspector, you can select which post types the user will be able to choose from.

Effect
Post typesSelect which post types the user will be able to choose from.
Only show published postsAlso shows draft posts if disabled.
Show filtersDisplay a select to filter by post type in the modal, and a select to filter by taxonomy when a post type is selected.
Sort orderSelect the suggested posts display order in the sidebar left column.
Limit selectionsIf enabled, select the maximum number of posts the user can select.
BidirectionalRecord the reverse relation, so a selected post knows which posts point at it (see below).
The available field options

Using the relation field

The field shows a preview of the selection made by the user. The “Select posts” button opens a modal for selecting other posts.

In the modal, there are 2 columns: the left column displays the available posts (based on the allowed post types), and the right column displays your selection.

The search bar lets you quickly find your posts.

You can then reorder the items in your selection by dragging and dropping them to set their display order.

Click on “Confirm selection” to exit the modal and update the selected posts.

Check the relation field in action through this video:

The relation field

Show posts with relation loop block

In Modern Fields, you don’t need to write code to display the selection. Instead, use the block “Relation loop“. you’ll find it in the inserter.

It’s a variation of the native WordPress query loop block, so it works the same way.

You just need to select the relation field in the block options in the inspector. If only one relation is available, it will be automatically selected.

It also works with a relation field in an option page, from a template file…

Now, when you change the selection in the relation field, the relation loop adapts itself live.

The relation loop block

Register a pattern for the relation loop block

You can declare a pattern dedicated to the relation loop block. This way, when you insert the block, you will be able to select a complete, ready-to-use layout.

Select a pattern for the relation loop

To do so, add the property Block Types in the pattern PHP file. Then, you’ll need to set the reference to the relation loop block variation: core/query/modern-fields/relation-loop

PHP
wp-content/my-theme/patterns/relation-loop.php

Note: in your pattern, the Query loop should be the first block of the markup.

Learn more about registering WordPress patterns with Full Site Editing.

Show posts with code

You can still write code to display your selection, for example within a custom PHP block.

Outside a custom block:

PHP

Inside a Modern Fields custom PHP block:

PHP
mf-blocks/my-block.php

Learn more about the WP Query here.

Bidirectional relations

A relation points one way: from the post holding the field to the posts it selects. Turn on Record the reverse relation, in the Bidirectional panel of the field settings, and each selected post also records which posts selected it.

For example, agencies select the destinations they sell. With a bidirectional field, a destination can list the agencies selling it. Turning it on also records the relations already saved, in the background, so you can enable it halfway through a project.

Read the reverse relation with modern_fields_get_linked_posts(): pass the name of the relation field, as set on the other posts, and the post being pointed at (the current post by default). It returns post IDs, oldest link first.

PHP

A field that is not bidirectional returns an empty array. Turning the option off erases the reverse relations it recorded.


Repeater field