Support

Account

Home Forums Add-ons Repeater Field Loop Repeater Based on Sub Field Values

Solved

Loop Repeater Based on Sub Field Values

  • I have a repeater that is used to create citations. I need to only show the citations based on the value of one of the sub fields

    For example there is a sub field called “application” and I only want to show the repeater fields that have a value of ‘Functional Assay’ listed in the application sub field.

    I found some information here: http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/ that appears to do what I am trying to achieve, but I could not figure out how to modify it to loop through text sub fields.

    If anyone has done this before or has any insight I would appreciate any help.

  • Not sure what you are looking for. Do you want to query posts and only return posts with the subfield value? Or do you want to loop through the sub field and show only the rows that contain the value? Or are you trying to do both of these?

  • Thank you for responding and sorry for not explaining the question well enough.

    I am only looking to loop through the sub field and show only the rows that contain the value of ‘functional assay’ within the ‘application’ sub field.

  • Hey Chris, try this out:

    <?php
    if(have_rows('field_name')):
       while(have_rows('field_name')): the_row();
          if(get_sub_field('sub_field') == 'functional assay'):
             the_sub_field('sub_field');
          endif;
       endwhile;
    endif;
    ?>

    Thanks John for the correction, I’ve edited. Dunno how I missed that one *whoosh*

    The only problem is if you have more than one of the same repeater subfield in a single post it doesn’t display the next one. My PHP knowledge is somewhat novice though so maybe another person can fix that! But this should nudge you in the right direction hopefully.

  • @chriswhiteley, the code posted by @bloodlvst should do what you want. Just change the field_name to the name of your repeater field and sub_field with your sub field name.

    and it will show every row with a value of functional assay if you want to only show the first one add a break; after the line the_sub_field('subfield');

    There is one error in the code

    
    <?php
    if(have_rows('field_name')):
       while(have_rows('field_name')): the_row();
          if(get_sub_field('sub_field') == 'functional assay'):
             the_sub_field('sub_field');
          endif;
       endwhile;
    endif;
    ?>
    
  • Thanks for the help gentlemen! For some reason I decided this was a difficult problem and overlooked the simplest solution.

  • I need another piece of help related to this code snippet.

    I need to be able to add a header above this code snippet which has to be conditional based on having rows. I cannot have it in the ‘while’ section as the while loops through all the rows, not just the ones marked ‘functional assay.’

    For the time being I have done this:

    <?php
    if(have_rows('field_name')):
       while(have_rows('field_name')): the_row();
          if(get_sub_field('sub_field') == 'functional assay'):
             echo '<h4 class="underline">Functional Assay</h4>';
    break;
          endif;
       endwhile;
    endif;
    ?>
    
    <?php
    if(have_rows('field_name')):
       while(have_rows('field_name')): the_row();
          if(get_sub_field('sub_field') == 'functional assay'):
             the_sub_field('sub_field');
          endif;
       endwhile;
    endif;
    ?>

    I’m using the repeater loop twice, and the first instance is basically a check for rows, shows the title then breaks after the first row to not show the title multiple times.I feel this could be done in a much cleaner way, but I am not sure how.

  • you could use an output filter then only echo if there something in it.

    
    <?php
    $html = '';
    ob_start();
    if(have_rows('field_name')):
       while(have_rows('field_name')): the_row();
          if(get_sub_field('sub_field') == 'functional assay'):
             '<h4 class="underline">Functional Assay</h4>';
             the_sub_field('sub_field');
          endif;
       endwhile;
    endif;
    $html = ob_get_clean();
    if ($html) {
      echo $html;
    }
    ?>
    
  • Hi everyone,
    I’m trying to achieve something similar except I want to loop through a subfield which is a choice selection.
    I want to display it by row but first by this subfield :
    CATEGORY
    value
    value
    CATEGORY
    value

    Where CATEGORY is the subfield and value are the other subfields of a row.
    I tear my hair between the row loop and the choices loop…
    Thanks in advance for any help !

  • Thanks a lot John !!

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

The topic ‘Loop Repeater Based on Sub Field Values’ is closed to new replies.