Support

Account

Home Forums ACF PRO update multiple nested repeaters programmatically

Solving

update multiple nested repeaters programmatically

  • Hey,

    I have a field that looks like this:

    field (group)
    ==== repeater1
    ======== text
    ======== repeater2
    ============ post object field
    ============ repeater3
    ================ number
    ================ taxonomy

    I’m writing a migration script and I need to populate the above fields.
    I tried add_row, add_sub_row, update_field but nothing works.
    I saw this answer in SO where it says you need to use update_field if there are no rows and add_row if there are.

    So I tried this:

    $res = update_field(
            // parent group field
            'field_5b0584cf8c831', 
            [
                // repeater1
                'ingredients_groups' => [
                    $group_row
                ]
            ],
            $post_id
        );

    and a row was added to ‘ingredients_groups’ but I don’t know how to add
    rows to the other nested repeaters.
    Do I need to specify the full path when calling update_field like this:

    update_field(
    'parent_group_field',
    ['repeater1'=>
        ['repeater2'=>
            ['repeater3'=>$row]
        ]
    )

    ?

    Thanks.

  • OK got it to work.
    I used update_field() to add the first row with all the sub fields and nested repeater as arrays.
    For the next rows I fetched the entire parent group field using get_field()
    and appended the row as array.

  • If this was me I would probably loop through all of the content you are getting and build the entire repeater with all rows and then just use update_field to store the entire thing and skip adding all of the other rows one at a time. You do not have to use add row.

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

The topic ‘update multiple nested repeaters programmatically’ is closed to new replies.