Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update repeater field if I have the array key to it?

I've found this code which is supposed to update a repeater field in ACF Wordpress. My repeater has 3 columns. I need to update only a certain field from one column in the repeater. How to do that with this code:

// save a repeater field value
$field_key = "field_3424324234";
$value = array(
    array(
        "sub_field_1"   => "Foo",
        "sub_field_2"   => "Bar"
    )
);
update_field( $field_key, $value, $post_id );

I need to say that I don't have a PRO license and I can't use update_sub_field().

like image 595
Ionut Avatar asked Oct 19 '25 16:10

Ionut


1 Answers

I will answer my question bellow. I used a function a little different than update_field, called update_sub_field. For this to work you need to have ACF Pro 5+. Jordi Nebot answer helped me a bit, but in my case I needed to add some extra variable($post_id). So here it is, hoping will help someone:

update_sub_field( array("repeater_field_key", $row+1, "repeater_sub_field_key"), $new_value, $post_id);

I added $row+1 because $row number count starts from 1, when $row start position starts from 0 when looping through repeater. Hope it makes sense.

like image 138
Ionut Avatar answered Oct 21 '25 05:10

Ionut