Support

Account

Home Forums Add-ons Repeater Field Check if $value does exist in any repeater subfield

Solved

Check if $value does exist in any repeater subfield

  • I have a $value of today’s date in PHP, which I’d like to check against a repeater field of dates.

    If today’s date is not in any of the sub_fields called “custom_date”, it should display a certain code, if any of the “custom_date” subfields does contain today, it should display other subfields from that row.

    I tried the following code, but this’ll always loop all rows, but I only need one set of data (either yes, one of the sub_fields contains $value, or no, none of them do).

    <?php
    
    // check if the repeater field has rows of data
    if( have_rows('dates') ):
    
     	// loop through the rows of data
        while ( have_rows('dates') ) : the_row(); ?>
    
            <?php if('$value' == get_sub_field('custom_date')){ ?>
    
            <?php echo 'true'; } else echo 'false';?>
    
        <?php endwhile;
    
    else :
    
        // no rows found
    
    endif;
    
    ?>
  • try something like this

    
    <?php
      
      $found = false;
      if (have_rows('dates')) {
        while(have_rows('dates')) {
          the_row();
          if ($value == get_sub_field('custom_date')) {
            $found = true;
            echo 'true';
          }
        } // end while have rows
      } // end if have rows
      if (!$found) {
        echo 'false';
      }
    ?>
    
  • That’s genius. Thanks so much!

  • I needed to do almost exactly the same thing as the original poster and luckily I found this thread.

    +1 Thanks John!

  • Hey John, thank you for example!

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

The topic ‘Check if $value does exist in any repeater subfield’ is closed to new replies.