Hi,
I am looking to create a repeater field with a post_object as a sub field. When I do this with the code below, I get a number of notices:
$session_client = array(
"key" => "acf_field_session_clients_client",
"label" => "Client",
"name" => "client",
"type" => "post_object",
"post_type" => array(
"cp-client"
),
"return_format" => "object",
'instructions' => "",
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => array(),
'allow_null' => 0,
'multiple' => 0,
'ui' => 1
);
acf_add_local_field(array(
"key" => "acf_field_session_clients",
"label" => "Clients",
"name" => "session_client",
"type" => "repeater",
"sub_fields" => array(
$session_client
),
"parent" => "acf_group_session",
'instructions' => "",
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => ''
));
The notices are (amongst others)
Undefined index: _name in wp-content/plugins/advanced-custom-fields-pro/includes/api/api-field.php:201
Undefined index: _prepare in wp-content/plugins/advanced-custom-fields-pro/includes/api/api-field.php:203
I have tried to create a repeater field with a simple text repeater which works fine as does a normal post_object field without a repeater which again works fine. Is there something in my code that is preventing me from creating a repeater with a post_object? Note that cp-client is a CPT and there are valid posts in the system (so it’s not a case of that there are no posts!)
The only thing that I see that is wrong is that all acf field keys must start with “field_” and you have “acf_field_”
Also, group keys should start with “group_” but I don’t know if that is a requirement or not, it very well could be.
More information can be found here, you’ll see that group keys must start with “group_” https://www.advancedcustomfields.com/resources/register-fields-via-php/#group-settings and field keys must start with “field_” https://www.advancedcustomfields.com/resources/register-fields-via-php/#field-settings
Thanks for looking into this,
I did as you suggested and renamed these as detailed below.
$session_client = array(
"key" => "field_session_clients_client",
"label" => "Client",
"name" => "client",
"type" => "post_object",
"post_type" => array(
"cp-client"
),
"return_format" => "object",
'instructions' => "",
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => array(),
'allow_null' => 0,
'multiple' => 0,
'ui' => 1
);
acf_add_local_field(array(
"key" => "field_session_clients",
"label" => "Clients",
"name" => "session_client",
"type" => "repeater",
"sub_fields" => array(
$session_client
),
"parent" => "group_session",
'instructions' => "",
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => ''
));
Just a note though, I had created 3 other field groups and related fields in the same format as above and none of these posed any problems. I assumed that the “field_” and “group_” keys were just conventions rather than required.
I did a bit of digging and placed the following code at line 201 of /includes/api/api-field.php to see what was happening as this was where the first notice was occurring:
if(!$field['_name']) print_r($field);
The array is printed out if “_name” is not set. I checked the other repeaters I had used in other areas and this is triggered in those places also. These repeaters store and retrieve data just fine despite “_name” not being set but these only have text fields as children rather than post_object.
I even flushed the database just in case there was soemthign lurking in there somewhere using WordPress Database Reset but the issue still persists.
The topic ‘Creating Repeater field with Post Object in 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.