Support

Account

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

Solving

How to add new row in repeater under some group programatically

  • I use acf pro and have a repeater in group, looks like this https://src.fdrv.guru/180209165216.jpg

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

    or

    $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.
    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 😬

  • 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 https://src.fdrv.guru/180210004359.jpg

  • This code doesnt work…

    Here how I use it https://src.fdrv.guru/180210124918.jpg

    Result https://src.fdrv.guru/180210124232.jpg

    Db looks like this after that code https://src.fdrv.guru/180210124339.jpg
    If I try to add row from admin and then update post the db looks like this https://src.fdrv.guru/180210124510.jpg

  • 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!

    add_row(
    	"field_5b606d31f4bb9",
    	array(
    		"field_5b607763dc795" => 100,
    		"field_5b606d3bf4bba" => "10.10.2018"													
    		),
    	$post->ID
    );
    
  • 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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.