I feel like this should be relatively easy but for some reason I'm lost.
I'm trying to access an associated posts ACF repeater field based on what row is being outputted. Here's the query in questions:
<!-- Loop the responses -->
<?php $linkedrace = get_field('taggedrace', false, false); $currentcandidate = get_field('id');
$argsx = array(
'post_type' => 'question_set',
'posts_per_page' => -1,
'post__in'=> $linkedrace,
);
$questions = new WP_Query($argsx);
?>
<?php if ( $questions->have_posts() ) : ?>
<?php while ( $questions->have_posts() ) : $questions->the_post(); ?>
<?php if( have_rows('questions') ): ?>
<div class="responseblock">
<?php while( have_rows('questions') ): the_row(); ?>
<div class="responsegroup">
<h5 class="race-results__data__headline"><?php the_sub_field('qgroupa'); ?></h5>
<?php if(get_sub_field('qgroupdes')) { ?><?php the_sub_field('qgroupdes'); ?><?php } ?>
<!-- Loop through response subset -->
<?php if( have_rows('questiongroup') ): ?>
<?php while( have_rows('questiongroup') ): the_row(); ?>
<?php $responsenum++; ?>
<?php $rows = get_field('responses', $current ); $specific_row = $rows[$responsenum]; $sub = $specific_row['response']; echo $sub; ?>
<div class="responserow">
<div class="atabled">
<div class="cell one">
<p><?php the_sub_field('qresponsequestion'); ?></p>
</div>
<div class="cell two">
<?php $position = get_sub_field('response'); if ($position == "Yes") { ?>
<p class="race-results__response race-results__response--yes"><?php inline_svg( 'partials/icons/icon-yes-response.svg' ); ?>Yes</p>
<?php } else if ($position == "No") { ?>
<p class="race-results__response race-results__response--no"><?php inline_svg( 'partials/icons/icon-no-response.svg' ); ?>No</p>
<?php } else { ?>
<p class="race-results__response race-results__response--none"><?php inline_svg( 'partials/icons/icon-no-response.svg' ); ?>No response</p>
<?php } ?>
</div>
<div class="cell three">
<?php if(get_sub_field('responsecomments')) { ?><a class="morecoms" href="#" data-contents="<?php the_sub_field('responsecomments'); ?>" id="morecomments"><i class="race-results__prompt fas fa-comment-dots"></i></a><?php } else { ?>– –<?php } ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; endif; wp_reset_query(); ?>
The $responsenum is set outside of the loop. As part of the while loop it's counted up -- all of which I can see works fine. Now I need to define $specific_row = $rows[X]; so that the array key is passed from the dynamic $responsenum. Just dropping $responsenum returns no results.
Andrea P answers:
what is $rows supposed to be? it doesn't seem to be defined anywhere...
maybe you wanted to do something like this?
$responsenum++;
$rows = get_field('questiongroup');
$specific_row = $rows[$responsenum];
$sub = $specific_row['response'];
echo $sub;
or if maybe you wanted to get the current row, you could do the loop something like this.
while( have_rows('questiongroup') ) : $row = the_row(true);
$specific_row = $row[$responsenum];
but then the array keys would be the names of the fields... are they numbers? maybe you want something like
$row[$responsenum . '_response']
then you say that $responsenum is set outside the loop. where? are you sure is correctly setup? what happens if you just print it out? like
var_dump($responsenum);
Andrew Clemente comments:
Thanks, I forgot I had $rows defined outside of this nested loop. Even if I moved it it does not make a difference.
$responsenum is a number -- just counting the rows to be associated back to the current posts ACF repeater values.
Essentially there's two post types -- one for "question sets" and one one candidates. In this case you're on a single candidate page. With an additional query I'm looping through the questions set from their corresponding post type where each question for a particular set is defined by custom fields in an ACF repeater. The purpose of what I'm doing here then is trying to grab the "responses" which are stored in the candidate/current post ACF repeater fields.
Ultimately once I can get the meta correctly it will be used to conditionally display different icons as to yes/no supports
Here's just printing the number to show that its counting correctly
Andrea P comments:
could you try and var_dump all the involved variables to see if something is not being correctly pulled, or maybe the key-names are different from what you're expecting?
like this
$rows = get_field('responses', $current);
is $rows correctly retrieved? what does it contain exactly? and is it $current setup correctly? I assume it should be the id of the current candidate post, but on top of your code I see you actually store the id as
$currentcandidate = get_field('id');
Cesar Contreras answers:
I think you just have to define the variable "responsenum" before the whyle cycle. Like this code based on your code:
<!-- Loop through response subset -->
<?php if( have_rows('questiongroup') ): ?>
<?php $responsenum=0; ?>
<?php while( have_rows('questiongroup') ): the_row(); ?>
......
and increase it before closing that cycle. like this code based on your code
......
<div class="cell three">
<?php if(get_sub_field('responsecomments')) { ?><a class="morecoms" href="#" data-contents="<?php the_sub_field('responsecomments'); ?>" id="morecomments"><i class="race-results__prompt fas fa-comment-dots"></i></a><?php } else { ?>– –<?php } ?>
</div>
</div>
</div>
<?php $responsenum++; ?>
<?php endwhile; ?>
Andrew Clemente answers:
I've also tried <?php $responsenum++; $current_rank = get_post_meta( 8183, "responses_{$responsenum}_response", true); echo $current_rank; ?>
but with no results. Again, using a number in place of the field name outputs the expected result