Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

advanced custom fields - insert post programmatically and update repeater fields

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!

like image 833
Andrea Avatar asked Dec 22 '25 20:12

Andrea


1 Answers

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 );
like image 98
Andrea Avatar answered Dec 24 '25 11:12

Andrea