Home Forums General Issues Deleting repeater rows Reply To: Deleting repeater rows

  • 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);