Support

Account

Home Forums Add-ons Repeater Field How do I programmatically delete all repeater rows?

Solving

How do I programmatically delete all repeater rows?

  • I have a repeater with a single field (image url), I want to delete them all so I can repopulate the repeater with new URLs, I’m currently using this:

    $images = get_field('images');
                    $index = 0;
                    foreach ($images as $image) {
                        delete_row('images', $index);
                        $index++;
                    }

    But that will not delete the first row as it seems the row index is a normal count (not starting at zero)

    Is there a better way to do this?

  • Hi elliottc, maybe you could delete that meta field with delete_post_meta. You should pass the post ID.

    
    delete_post_meta($post_id, 'images');
    
  • Just change the counter to start at 1

    
    $index = 1;
    

    or do the loop in a different way

    
    $images = get_field('images');
    if (!empty($images)) {
      $count = count($images);
      for ($index=1; $index<=$count; $index++) {
        delete_row('images', $index);
      }
    }
    
  • Hi John, Thanks for your code.
    I had trouble with this today, not all rows were being removed.
    I needed to remove them in reverse order:

    
            $existing_locations = get_field('rp_locations', $post_id);
            if( !empty($existing_locations) )
            {
                for( $index = count($existing_locations); $index > 0; $index-- )
                {
                    delete_row('rp_locations', $index, $post_id);
                }
            }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.