Hey Guys,
I want to add new repeater rows. But before I do that, I want to delete all repeater rows but I didn’t found a function for that…
I tried to use update_field(...)
on the repeater with an empty array and then add_row(...)
and also update_field(array(...))
. Doesn’t work…
Because I do not insert the same amount of rows again, I cannot just use the update function for the subfields. I need to delete all repeater rows and then add all …
How can I do that and which one is the easiest and cleanest way?
Greetings and Thank You!
Hi @frazecolder ,
You can achieve the same by updating the field using update_field as below.
if (have_rows(‘repeater_field’, $post_id)) {
$new_repeater = array();
while (have_rows(‘repeater_field’, $post_id)) {
the_row;
// perform check
// will depend on what you’re checking for
if (get_sub_field(‘the_sub_field_to_check’) == $some_value) {
// the value is one you want to save
$new_repeater[] = array(
‘sub_field_1_name’ => get_sub_field(‘sub_field_1_name’),
‘sub_field_2_name’ => get_sub_field(‘sub_field_2_name’),
‘sub_field_3_name’ => get_sub_field(‘sub_field_3_name’),
);
} // end if check field
} // end while have rows
update_field(‘repeater_field’, $new_repeater, $post_id);
} // end if have rows
In my case, I just wanted to complete empty my repeater field so I tried “delete_field($field_key, $post_id);” because I am constructing an array and i am using add_row to put the array into the field. My setup got a little complicated with repeater inside of repeater inside of repeater. Seemed the best approach was construct an array and use add_row.
The delete_field seemed to do the trick, but is that a good way to completely remove all values from a repeater? That is exactly what I wanted it to do. Tested locally and appears to work.
All other methods like delete_row() didn’t seem to work, it was always left with something in the repeater. I tried looping through and deleting rows, still same issue, it always left something behind.