Support

Account

Home Forums Add-ons Repeater Field wp_insert_post and repeater fields Reply To: wp_insert_post and repeater fields

  • 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 );
    }