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


How to add new row in repeater under some group programatically

  • I use acf pro and have a repeater in group, looks like this

    I try to insert post in db, and during the process I want to add new row to repeater. What is the right way to do it?

    update_field(‘attributes_cargo_pick’, array( array(‘pcs’ => 1, ‘bol’ => 2, ‘size’ => 3)), $id);


    $row = array(
    ‘pcs’ => 1,
    ‘bol’ => 2,
    ‘size’ => 3
    add_row(‘attributes_cargo_pick’, $row, $id);

    or sub row or use field key like field_5a6b8a18b552e

    it is all doenst work…

  • G’day mate,

    The proper function you are looking for to add/edit a entry to the repeater field is called update_row.

    And, the proper hook you are looking for is acf/save_post, which gets triggered after the entered acf values are saved.

    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 😬

  • I dont want to insert the row after save. I create a new post programatically and redirect him to edit this post screen, and I want repeater will be already with a row.

    Here How I use it

  • This code doesnt work…

    Here how I use it


    Db looks like this after that code
    If I try to add row from admin and then update post the db looks like this

  • I have the same problem. I have a repeater inside a group. I can’t add new rows to the repeater, programatically. I have tried using add_row with field names or keys. add_row returns integers but in the backend I can’t see any values.

    I have tested also with a repeater outside the group. On that it works using add_row.

    Thanks for any help!

    		"field_5b607763dc795" => 100,
    		"field_5b606d3bf4bba" => "10.10.2018"													
  • I have found a solution:

    We can use update_field to add new rows to a repeater inside a group.

    1. Get all values of the fields inside that group
    2. Manipulate the resulting array adding new values in the repeater’s array
    3. Update the group field.

    It works even if the repeater never had any rows.

    $pachet_8 = get_field("pachet_8", $post_id);
    $pachet_8['rata_3']['plati'][] = array(
    													"suma" => 1001,
    													"data_platii" => "10.10.2018"															
    update_field("pachet_8", $pachet_8, $post_id);
  • Omg dude, I’m so thankful for your solution, works great.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘How to add new row in repeater under some group programatically’ is closed to new replies.