Support

Account

Home Forums Add-ons Repeater Field How to add new row in repeater under some group programatically Reply To: How to add new row in repeater under some group programatically

  • G’day mate,

    The proper function you are looking for to add/edit a entry to the repeater field is called update_row.
    https://www.advancedcustomfields.com/resources/update_row/

    And, the proper hook you are looking for is acf/save_post, which gets triggered after the entered acf values are saved.
    https://www.advancedcustomfields.com/resources/acf-save_post/

    To combine the above 2 resources, your code should look something similar like:

    
    add_action('acf/save_post', 'insert_that_very_special_row');
    function insert_that_very_special_row($post_id) {
    	// get how many rows is already in the database first, 
    	// cause we need to insert our row by index
    	$row_count = count(get_field('attributes_cargo_pick', $post_id)?: []);
    
    	// the new row that we wanna add
    	$row = ['pcs' => 1, 'bol' => 2, 'size' => 3];
    
    	// update it as the next index (++$row_count)
    	update_row('attributes_cargo_pick', ++$row_count, $row, $post_id);
    }
    

    Cheers 😬