Relationship field
Purpose
An ordered list of posts: related articles, team members, featured products. Editors search, add, remove and drag items into order.
Field type name
relationship
Settings
| Setting | Description |
|---|---|
| Instructions | Shown to editors below the field label. (max 2000 characters) |
| Post Types | Post types editors can choose from (e.g. ["page", "product"]). Empty = every public post type. |
| Post Status | Only offer posts with these statuses (e.g. ["publish"]). Empty = any status except trash and auto-draft. |
| Filter by Taxonomy | Only offer posts in one of these terms, written taxonomy:slug (e.g. category:news). Applies to the picker. |
| Minimum Items | Fewest items a non-empty value may have. |
| Maximum Items | Most items editors may choose (hard limit 200). |
| Return Format | object (WP_Post) or id. |
| Translation | How the field behaves in other languages: translate, copy, copy_once or do_not_translate. Used on top-level fields when WPML is active. See WPML. |
| Width | Width on edit screens, to place fields side by side: 100 (full), 75, 66, 50, 33 or 25 (%). Phones always show full width. |
| Label per language | The label and instructions per language code while editing in that language. Set on the Translation tab when WPML is active. See WPML. |
PHP return value
Always a list of WP_Post (or IDs) in the chosen order; an empty list when nothing is stored. Deleted or trashed posts drop out; order is kept.
Example
foreach ( cafe_get_field( 'related' ) ?? [] as $post ) {
printf( '<li><a href="%s">%s</a></li>', esc_url( get_permalink( $post ) ), esc_html( get_the_title( $post ) ) );
}
Editing
Relationship uses a two-panel picker: the list with a filter box on one side, the
chosen items on the other: numbered, sortable by dragging, with a count (2 / 5 when
Maximum is set). Clicking an item adds it; chosen items are ticked in the list.
On narrow screens the panels are stacked.
The chosen items and the first 20 items of the list are printed with the edit screen,
so the list opens at once, with no request. When those 20 are all there is, typing
filters them in the browser (case- and accent-insensitive) and the server is never
asked. Longer lists are searched on the server as the editor types (debounced, 20 per
page, "Load more" for the next page) through
GET /cafe/v1/references?field={key}&search=…&page=…; the current list stays visible
with a "Searching…" row until the answer arrives. The object being edited is never
offered. See REST.