On a post, I check for other posts which contain certain values and add those to a repeater field on the current post. As I don’t want duplicates, I want to delete all rows in the repeater before updating it.
I use the following code:
if (have_rows($field_key, $thisPost)):
while (have_rows($field_key, $thisPost)):
delete_row($field_key, get_row_index(), $thisPost);
But for some reason, it doesn’t delete all repeater rows. If I run the above through a for loop with 3 iterations it works, but that obvously is a shitty hack.
This is because when you delete the first row (index 0) the second row (index 1) becomes the first row (index 0), the 3rd row (index 2) becomes the 2nd row (index 1), etc. The next iteration of the loop moves on to index 1 and does not repeat index 0, so the loop will only delete every other row.
What you need to do is to get the count of the repeater and loop through it backwards