Support

Account

Home Forums Add-ons Repeater Field Query if a Repeating Field Group has a field with specific value.

Helping

Query if a Repeating Field Group has a field with specific value.

  • I’m trying to set up a shortcode that displays a group of tabs created in a Flexible Content layout.

    Essentially the author should be able to add the tab content to the custom fields then use the shortcode to drop the tabs anywhere they like. I’ve got it working fine if there is is only one set of tabs, but i’d like for them to be able to add mutliple multiple tab groups.

    Here’s my code so far.

    <?php
    function Foundation_Tabs($atts) { 
      extract(shortcode_atts(array(
    
     
        ), $atts));
      
    // check if the flexible content field has rows of data
    if( have_rows('foundation_elements') ):
    
      // loop through the rows of data
        while ( have_rows('foundation_elements') ) : the_row();
    
        // check current row layout
            if( get_row_layout() == 'foundation_tabs' ):
    
              // check if the nested repeater field has rows of data
              if( have_rows('tab') ):
                  ?>
                  
                  <ul class="tabs" data-tab>
                  <?php $title_count = 0; 
                  // loop through the rows of data
                    while ( have_rows('tab') ) : the_row();
    
                    $tab_title = get_sub_field('tab_title');
                  ?>
    
                    <li class="tab-title<?php if ($title_count == 0) {echo' active';} ?>">
                      <a href="#panel<?php echo $title_count + 1; ?>"><?php echo $tab_title ?></a>
                    </li>
                    <?php $title_count++; ?> 
    
                  <?php endwhile;?>
                  </ul>
                  <div class="tabs-content">
                  <?php $content_count = 0; 
                  // loop through the rows of data
                    while ( have_rows('tab') ) : the_row();
    
                    $tab_content = get_sub_field('tab_content');
                  ?>
                    <div class="content<?php if ($content_count == 0) {echo' active';} ?>" id="panel<?php echo $content_count + 1; ?>">
                      <?php echo $tab_content; ?>
                    </div>
                    <?php $content_count++; ?> 
    
                  <?php endwhile;?>
                  </div>
                  <?php
                
              endif;
    
            endif; 
    
        endwhile;
    
    else :
    
        // no layouts found
    
    endif;
    
    ?>
    <?php
    }
    add_shortcode('tabs', 'Foundation_Tabs');
    ?>

    As I said this works fine and I can drop [tabs] into my content and it will display the tab groups, if I have multiple tabs then it displays all of them. What I’d like to be able to do is query the field “tab_group_name” and match that to a short code attribute; for example if the tab_group_name was “Tabs 1” the the short code would be [tabs ‘Tabs 1’]. I cannot work out how to query the ‘tab_group_name’ field and match it to the $atts.

    Screen shop of the Flexible Field Group attached.

  • Hi @nday,

    Thanks for the post.

    The ACF resource page contains a tutorial showcasing the process of querying the database for repeater sub field values.

    The same can be applied to a Flexible content field since the save format is almost similar.

    You can have a look at this tutorial here: https://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/

    I hope this helps.

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

The topic ‘Query if a Repeating Field Group has a field with specific value.’ is closed to new replies.