2
votes

I've an issue with the plugin "Advanced Custom Fields". The steps that I follows are:

1. Create post programmatically

$id_post = wp_insert_post(array(
    'post_type'=>$post_type,
    'post_title'=>$post_title, 
    'post_status' => 'publish'
));

2. Update all the repeater fields associated to the "post_type"

if( have_rows('cliente',$id_post) ) {
    $i = 0;
    while( have_rows('cliente',$id_post) ) {
       the_row();
       update_sub_field('id', 333);
    }
}

The issue is at the point 2, infact when I create a post with Wordpress' interface (with a button) and i insert manually the id of that post in my code, it works perfectly..

but when i create a post programmatically at the second point the repeater field isn't recognized even if I put the number of that post create programmatically.

Works only if the post is create with the button "Insert New".

Do you have any suggestion?

Thanks to all!

2
I've resolved the problem! The issue regards the name of the fields.. you must use the key instead of the name. My code now is this $cliente_data = array( array( "id" => 33 ) ); update_field('field_582c2ed4fab65', $cliente_data, $id_post );Andrea
Thank you for your comment! You saved my day.Gleb Kemarsky
@Andrea Could you please make your comment the accepted answer with an example.Rauli Rajande
@RauliRajande done! Let me working on the example since 2 years have been passed :))Andrea

2 Answers

2
votes

I've resolved the problem! The issue regards the name of the fields.. you must use the key instead of the name. My code now is this

$cliente_data = array( array( "id"  => 33 ) ); 
update_field('field_582c2ed4fab65', $cliente_data, $id_post );
1
votes

Please refer the tutorial which explains create and update fields programmatically. http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

$newIds = wp_insert_post( array(
            'post_title' => $postCSVContent['1'],
            'post_content' => $postCSVContent['2'],
            'post_type' => 'doors',
            'post_status' => 'publish',        
            'post_author'   => 1,
            'post_parent' => $parentId
    ));

    updateAcf( $postCSVContent , $newIds );

Update acf image repeater fields : http://www.pearlbells.co.uk/insert-update-acf-image-repeater-field-programmatically/