Skip to main content

Options

Metaboxes that require options all use the same signature. The options parameter can be either an array, a multi-dimensional array, a function or a simple string (post type).

String

You can specify post type/s as the option to get a list of available posts from this post type.

options => 'page'
options => 'cpt_1, cpt_2, cpt_3' // Comma separated list
options => 'all' // List all post types

Array

options => array('option 1', 'options 2');

Multi-dimensional Array

Allows for user friendly selections while the value may just represent an ID or something not useful to the end user.

options => array(
array(
'id' => '01',
'title' => 'Option 1'
),
array(
'id' => '02',
'title' => 'Option 2'
)
);

Function

You can pass an anonymous function as the option which will be executed at run time, rather than when parsed. This may be useful if you need to reference something that is created during init, i.e. custom taxonomies.

options => function() {
$options = get_terms([
'taxonomy' => 'custom_taxonomy_slug',
'hide_empty' => false,
]);

return array_map(function($option) {
return array(
'id' => $option->term_id,
'title' => $option->name,
);
}, $options);
}