Home › Forums › Backend Issues (wp-admin) › Post Object Field from Generated PHP
I’ve managed to get the code to work, but doesnt’ seem like the post_object
type that I’ve created matches the Post Object
type from the ACF Field creation admin.
Here’s the code:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_1',
'title' => 'My Group',
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'acf-options',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
));
acf_add_local_field( array(
/* (string) Unique identifier for the field. Must begin with 'field_' */
'key' => 'field_1',
/* (string) Visible when editing the field value */
'label' => 'Sub Title',
/* (string) Used to save and load data. Single word, no spaces. Underscores and dashes allowed */
'name' => 'sub_title',
/* (string) Type of field (text, textarea, image, etc) */
'type' => 'post_object',
'parent' => 'group_1',
/* (string) Instructions for authors. Shown when submitting data */
'instructions' => '',
/* (int) Whether or not the field value is required. Defaults to 0 */
'required' => 0,
/* (mixed) Conditionally hide or show this field based on other field's values.
Best to use the ACF UI and export to understand the array structure. Defaults to 0 */
'conditional_logic' => 0,
/* (array) An array of attributes given to the field element */
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
/* (mixed) A default value used by ACF if no value has yet been saved */
'default_value' => '',
/* (mixed) Specify an array of post types to filter the available choices. Defaults to '' */
'post_type' => 'post',
/* (mixed) Specify an array of taxonomies to filter the available choices. Defaults to '' */
'taxonomy' => '',
/* (bool) Allow a null (blank) value to be selected. Defaults to 0 */
'allow_null' => 0,
/* (bool) Allow mulitple choices to be selected. Defaults to 0 */
'multiple' => 0,
/* (string) Specify the type of value returned by get_field(). Defaults to 'object'.
Choices of 'object' (Post object) or 'id' (Post ID) */
'return_format' => 'object',
) );
endif;
The first one allows multiple selections, the second one does not. Change this to 1
/* (bool) Allow mulitple choices to be selected. Defaults to 0 */
'multiple' => 0,
The topic ‘Post Object Field from Generated PHP’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.