🎉 Launch Party!

discount for all licences until Aug 31.

Buy now →

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 versions 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.
Published posts onlyAlso shows drafts 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 selectionIf enabled, select the maximum number of posts the user can select.
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.


Repeater field