Home › Forums › Add-ons › Repeater Field › wp_insert_post and repeater fields
I’m using a plugin which uses the wp_insert_post function to create posts from user input. My posts in particular contain ACF fields. Fields which take a single value are no problem. But I’m having trouble populating repeater fields.
Let’s I have a repeater field, “my-class”, which may contain any number of sub-fields, “my-class-item”. The user fills in a form and we get an array for “my-class-item” (e.g. array(‘class 1′,’class 2′,’class 3’)).
Something like the following is sent to the back end using wp_insert_post…
$some_post = array(
'post_title' => 'Title',
'post_content' => 'Content',
'meta_my-name' => 'Tom', // ACF text field
'meta_my-city' => 'New York', // ACF text field
'meta_my-class-item' => array('class 1','class 2','class 3') // ACF repeater sub-field
);
I know that the above code won’t work. But I hope that it communicates what I’m trying to do. Are there any suggestions?
Hi @friendofdog
I’m not sure if you already now this, but ACF has acf_form() to create a form on the front end. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/create-a-front-end-form/.
I hope this helps.
Thanks for pointing me towards those resources. I pieced things together and came up with the following…
$field_key = 'field_123123123123';
$subfield_key = 'field_456456456456'; // corresponds with my-class-item in my previous post
$post_id = $the_post_id; // gets current post ID
$items = array('class 1','class 2','class 3');
foreach ($items as $items_value) {
$value[] = array($subfield_key => $items_value);
update_field( $field_key, $value, $post_id );
}
It’s not a game changer, but is there any way to use the name of the custom field instead of the field key (e.g. “my-class-item” instead of “field_456456456456”)?
Hi @friendofdog
Could you please tell me why you don’t want to use the acf_form() instead?
You can use the field name as long as the reference for a value already exists. Please take a look at this page to learn more about it: http://www.advancedcustomfields.com/resources/update_field/#field_key-vs field_name
I hope this helps.
Hi James,
Ends up that I cannot use the field names, for the reason that you mentioned.
I want to use acf_form() but I couldn’t get it to work for me. It’s not really a big issue, as I found a workaround.
Below is the end product. I’m pretty satisfied with this (in that it works), but any further suggestions/comments are appreciated.
$some_post = array(
'post_title' => 'Title',
'post_content' => 'Content',
'post_content' => '',
'post_status' => 'publish'
);
$the_post_id = wp_insert_post($some_post);
update_field('field_111111111111', 'Tom', $the_post_id);
update_field('field_222222222222', 'New York', $the_post_id);
// repeater field with one subfield
$classs_field_key = 'field_3a3a3a3a3a3a';
$classs_subfield_key = 'field_3b3b3b3b3b3b';
$classs_items = array('class 1','class 2','class 3');
foreach ($classs_items as $classs_items_value) {
$classs_value[] = array($classs_subfield_key => $classs_items_value);
update_field($classs_field_key, $classs_value, $the_post_id);
}
// repeater field with multiple subfields
$fruit_field_key = 'field_4a4a4a4a4a4a';
$fruit_subfield_name = 'field_4b4b4b4b4b4b';
$fruit_subfield_colour = 'field_4c4c4c4c4c4c';
$fruit_names = array('Banana','Pear','Grape');
$fruit_colours = array('yellow','green','purple');
foreach ($fruit_names as $index => $fruit_names) {
$fruit_value[] = array($fruit_subfield_name => $fruit_names, $fruit_subfield_colour => $fruit_colours[$index]);
update_field( $fruit_field_key, $fruit_value, $the_post_id );
}
When importing several CPTs, this was accumulating row fields values.
Updated like this:
$the_values = null;
foreach ($years as $index => $years) {
$the_values[] = array(
$subfield_key_1 => $years,
$subfield_key_2 => $tax_01[$index],
$subfield_key_3 => $tax_02[$index],
$subfield_key_4 => $tax_03[$index],
);
update_field($field_key, $the_values, $post_id);
}
The topic ‘wp_insert_post and repeater fields’ 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.