Home Forums General Issues Deleting repeater rows


Deleting repeater rows

  • Hey guys,

    I’m trying to do the following:

    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.

    Any suggestion why this happens?

  • 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

    $rows = get_field($field_key, $thisPost));
    if (!empty($rows)) {
      $count = count($rows);
      for ($x=count; $x>0; $x--) {
        $index = $x-1;
        delete_row($field_key, $index, $thisPost);
  • I knew it had to be something easy. Had to modify it tho because I still had the problem with duplicating rows.

    $getRows = get_field($field_key, $thisPost);
    if(!empty($getRows)) {
    	for($i = count($getRows); $i > 0; $i--) {
    		delete_row($field_key, $i, $thisPost);

    Thanks for helping!

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

You must be logged in to reply to this topic.