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


Check if $value does exist in any repeater subfield

    • Teun

    • September 1, 2016 at 7:19 pm

    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).

    // 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
  • try something like this

      $found = false;
      if (have_rows('dates')) {
        while(have_rows('dates')) {
          if ($value == get_sub_field('custom_date')) {
            $found = true;
            echo 'true';
        } // end while have rows
      } // end if have rows
      if (!$found) {
        echo 'false';
    • Teun

    • September 2, 2016 at 4:41 pm

    That’s genius. Thanks so much!

    • Femto

    • February 26, 2018 at 3:33 pm

    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)

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.