Post

Improve this page

Overview

The post field allows you to select one or multiple post (or any custom post type) objects. This field has several settings that can be displayed as a: simple select dropdown, checkbox list, or beautiful select dropdown with select2 library.

If the post type is hierarchical, you are able to display the field as a select or checkbox tree, e.g. show childen posts when parent post is selected.

Screenshots

Beautiful select with select2 library (default):

post select

Checkbox list:

post checkbox list

Checkbox tree:

post checkbox tree

Radio list:

radio list

Radio list inline:

radio list inline

Select tree:

post select tree

Settings

Besides the common settings, this field has the following specific settings:

Name Description
post_type Post type. Can be string (for single post type) or array (for multiple post types). Required.
query_args Query arguments for getting post objects. Uses same arguments as WP_Query. Optional.
parent Whether or not to set the selected post as the parent for the current being edited post? true or false (default).
placeholder The placeholder for the select box. Default is “Select a {post label}”. Applied only when the field_type is a select field.
field_type How the posts are displayed? See below.

This field inherits the look and field (and settings) from other fields, depending on the field_type, which accepts the following value:

Field type Description Settings inherited from
select Simple select dropdown. select
select_advanced Beautiful select dropdown using the select2 library. This is the default value. select_advanced
select_tree Hierachical list of select boxes which allows to select multiple items (select/deselect parent item will show/hide child items). Applied only when the post type is hierarchical (like pages). select
checkbox_list Flatten list of checkboxes which allows to select multiple items. checkbox_list
checkbox_tree Hierachical list of checkboxes which allows to select multiple items (select/deselect parent item will show/hide child items). Applied only when the post type is hierarchical (like pages). checkbox_list
radio_list Flatten list of radio boxes which allows to select only 1 item. radio

Note that for select_tree, checkbox_list, checkbox_tree, the multiple setting is always set to true.

Sample code

This code shows pages in select2 dropdown:

array(
    'name'        => 'Select a page',
    'id'          => 'page',
    'type'        => 'post',

    // Post type.
    'post_type'   => 'page',

    // Field type.
    'field_type'  => 'select_advanced',

    // Placeholder, inherited from `select_advanced` field.
    'placeholder' => 'Select a page',

    // Query arguments. See https://codex.wordpress.org/Class_Reference/WP_Query
    'query_args'  => array(
        'post_status'    => 'publish',
        'posts_per_page' => - 1,
    ),
),

The code below shows pages in a checkbox tree format:

array(
    'name'        => 'Select a page',
    'id'          => 'page',
    'type'        => 'post',

    // Post type.
    'post_type'   => 'page',

    // Field type.
    'field_type'  => 'checkbox_tree',
),

Data

This field saves post ID(s) in the database.

If field is not multiple, then a single post ID is saved in the database. Otherwise, the field saves multiple post IDs in the database, where each post ID is store in a single row in the database with the same meta key (similar to what add_post_meta does with last parameter false).

If the field is cloneable, then the value is stored as a serialized array in a single row in the database.

If parent settings is set to true, this field doesn’t save any data. Instead, it sets the selected post as the parent for the current being edited post.

Template usage

If field is not multiple, getting value is simple:

$post_id = rwmb_meta( $field_id );
echo get_the_title( $post_id );

If field is multiple, you can loop through the returned values like this:

$post_ids = rwmb_meta( $field_id );
foreach ( $post_ids as $post_id ) {
    echo '<p>', get_the_title( $post_id ), '</p>';
}

If you just want to output selected posts in an unordered list, use this code:

rwmb_the_value( $field_id );

Read more about rwmb_meta() and rwmb_the_value().