Support

Account

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

Solving

delete_row() with WPML

  • Hello,

    I’ve having an issue trying to delete a row within a Flexible Content -> Repeater subfield. What I need to do is if a sub_field to the Repeater doesn’t exist remove the entire row from the Repeater. I found on the documentation that delete_row() should work but it’s simply not no matter if I use the field name or the key. I’ve also tried to use delete_sub_row() also to no avail.

    I know there are some issues with flexible content and WPML but I’m starting to believe that there is an issue here as well. Has anyone run into this issue before as well or am I doing something wrong?

    Obviously, I’ve trimmed this code down but the information below is what I’m having issues with.

    if ( get_row_layout() == ‘grid_large’ ):
    if ( have_rows(‘blocks’) ) :
    while ( have_rows(‘blocks’) ) : the_row();
    $link = get_sub_field(‘link’);
    if(!$link){
    delete_row(‘blocks’, $i);
    }

  • I do not see where you are setting $i in your code, but you could have removed it

    
    $i = get_row_index();
    delete_row('blocks', $i);
    
  • John,

    Yes, I removed that bit from the code. However, I set it to 1 since I read that it starts off at 1. Then I obviously incremented it in the loop.

    Should I have used the get_row_index function instead?

  • John,

    I tried that solution as well even though I believe I tried that previously but it’s a no-go. Also, the index is always set to one for some reason. I’m also going to attach the entire file just to see if you see something else. The code to focus on is around line #:420.

    I’m leaving in all the comments and randomness that I was trying as well. I’d expect the delete_row function to delete something even if the index number was correct, but nothing is being removed.

    I appreciate it.

  • 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);
            }
          }
        }
      }
    }
    
  • Thanks, John,

    I’ll take a stab at this approach. I figured the indexes would change or the row that I deleted would be blank and I’d have to reindex at the end as I found a function to do that; having issues finding it now though. However, I wanted to get step one working first which is to remove the unwanted row.

    I really appreciate the assistance.

Viewing 6 posts - 1 through 6 (of 6 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.