Support

Account

Home Forums Add-ons Flexible Content Field delete_row() with WPML Reply To: delete_row() with WPML

  • I just did a test and I mostly got it to work using this code, but it does not really work.

    I will explain. When you delete a row then then indexes all change. For example if you delete index 1 then index 2 is moved to index 1. The next loop will load index 2 which used to be index 3. So index 2 which is now index 1 is never used.

    Confused?

    The best choice would be to make a list of all of the indexes that need to be deleted and then delete them in reverse order to make sure you do not interfere with the operation of the loop or delete the wrong ones.

    
    if (have_rows('flex_field')) {
      while (have_rows('flex_field')) {
        the_row();
        $flex_index = get_row_index();
        if (have_rows('repeater')) {
          while (have_rows('repeater')) {
            the_row();
            $repeater_index = get_row_index();
            if (empty(get_sub_field('test_field'))) {
              delete_sub_row(array('flex_field', $flex_index, 'repeater'), $repeater_index);
            }
          }
        }
      }
    }