Support

Account

Home Forums Add-ons Repeater Field If repeater subfield has this value in any row : do something

Solved

If repeater subfield has this value in any row : do something

  • Hi,

    I’m trying to do something that I thought was fairly basic, but I couldn’t find the solution…

    In my repeater I have a true/false subfield. If in at least one row this checkbox is on TRUE, I want to show something, but just once (so not in a foreach)

    Basically this is my code, I know it isn’t correct but i tried many different things without finding the right syntax.

    <?php
    $rows = get_field('repeater');
    $optionsonoff = $rows['optionstruefalse'];
    if( $rows ) {
    if ( $optionsonoff !== '' ) { ?>my html<?php }} ?>
  • The only way that you can find if any row has the true false field set to true is to loop over the entire repeater and test every rows. When you use

    
    $rows = get_field('repeater');
    

    the format of $rows will be something like

    
    $rows = array(
      // each row is a nested array
      array(
        // each subfield is a key => value pair
        'sub_field' => 'value'
      )
    )
    
    
    $has_true = false;
    foreach ($rows as $row) {
      if ($row['sub_field'])) {
        $has_true = true;
        break;
      }
    }
    
    if ($has_true) {
      // do something
    }
    
  • Hi John,

    Thank you very much, worked fine.

    Have a good day.

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

You must be logged in to reply to this topic.