Home Forums Add-ons Repeater Field Accessing sub fields from multiple repeaters


Accessing sub fields from multiple repeaters

  • I’m trying to make an if situation where if a sub field from repeater 2 is entered, ignore the sub field from repeater 1 and display repeater 2’s field.

    When I try to write it like below, it shows multiple blank rows (content of repeater 2). How would I write it so that it shows the contents of repeater 2 properly?

    						if( have_rows('breakfast_mf_regular_price')  ) : 
    						while ( have_rows('breakfast_mf_regular_price') ) : the_row();
    							if( have_rows('breakfast_mf_special_price')  ) : 
    							while ( have_rows('breakfast_mf_special_price') ) : the_row();
    						<?php if(!$today < $special_end && get_field('breakfast_mf_special_price')) : ?>
    							<div class="buffet-group"><?php echo get_sub_field('breakfast_mf_group_type_special'); ?> $<?php echo get_sub_field('breakfast_mf_group_price_special'); ?></div>
    						<?php else: ?>
    							<div class="buffet-group"><?php echo get_sub_field('breakfast_group_type_regular'); ?> $<?php echo get_sub_field('breakfast_group_price_regular'); ?></div>
    						<?php endif; ?>
    					endif; ?>
  • There isn’t a way to do this using have_rows loops. To do this you would need to get both of the repeater fields as arrays and loop through them, something like

    $regular = get_field('breakfast_mf_regular_price');
    $special = get_field('breakfast_mf_special_price');

    both of these will hold an array of rows. But since there’s no guarantee that the rows of the second repeater will align with the rows of the first repeater, there really isn’t going to be an easy way to check to see which value you should use.

    Basically you need to loop through one of them and then loop through the other. This is a really basic example of plucking one sub field out of one of two arrays.

    foreach ($regular as $r_row) {
      $price = $r_row['price'];
      foreach ($special as $s_row) {
        if ($r_row['name'] == $s_row['name']) {
          $price = $s_row['price'];
  • I changed my code up to the above, but now it doesn’t display the special price…still just the regular.

    							foreach ($breakfast_weekday_regular as $bwdr_row) {
    								$price = $bwdr_row['breakfast_group_price_regular'];
    								foreach ($special as $s_row) {
    									if ($bwdr_row['breakfast_group_type_regular'] == $s_row['breakfast_group_type_special']) {
    										$price = $s_row['breakfast_group_price_special'];
    								echo '<div class="buffet-group">' . $bwdr_row['breakfast_group_type_regular'] . ' ' .$price . '</div>';
  • There is probably something in the matching up of one row against the other. That part of my code was pretty much a guess on my part. You’re going to need to look at the values stored in each repeater and figure out how to match up one with the other.

    I would probably try to find a way to use a single repeater, or a nested repeater. Matching one repeater against another one is going to be prone to errors and problems when it comes to matching the content in them.

  • Yes I think the only way to do it is to put them all in the same repeater. I will rewrite it. thanks

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

The topic ‘Accessing sub fields from multiple repeaters’ is closed to new replies.