Home › Forums › General Issues › Looping through a group with a nested repeater
Hi there, I have tried to post a question several times in the help forum but I keep getting a “you have been blocked” message. Is there a new policy on what is and isn’t allowed? I can track the issue to putting my code in. I make sure to wrap it in /code but it has blocked me 3x.
Here is the issue I am having and the code I am trying to get help with. Thanks!
Can anyone help me with this? I have tried writing this code every way I can think of with no luck. Basically, I have a group with a nested repeater on my CPT, Books. Inside my repeater is a call for videos and their title. I don’t want to show the div (class med) unless there are rows in my nested repeater but if I move any of the code below around, everything stops working. I would appreciate another set of eyes. Thank you so much!
query_posts(
array( // loop thru books
'post_type' => books,
'post_status' => publish,
'showposts' => 5000,
'order' => 'DESC',
)
);
if ( have_posts() ) :
if ( have_rows('book_videos') ): // this is my group
echo '<div class="med"><h3>Book Video Series</h3><ul class="b-articles">'; //if I move this further down, it will repeat for each post in the CPT
while (have_posts()) : the_post(); //the loop that refuses to go anywhere else
while ( have_rows('book_videos') ): the_row(); //the group
if ( have_rows('bk_videos') ): while ( have_rows('bk_videos') ): the_row(); // the nested repeater
echo '<li>';
echo '<p>';
echo the_sub_field('video_title');
echo '</p>';
echo '<a class="book-title" href="';
the_permalink();
echo '">';
echo the_title();
echo '</a>';
echo '</li>';
endwhile; // main book while
endif; // bkvideos
endwhile; endwhile; echo '</ul></div><!-- media-div -->';
endif;
wp_reset_query(); endif;
Your opening of the main while loop where your comment is “the loop that refuses to go anywhere”, when you’re moving it, are you also moving its closing statements? That should go after your opening:
if ( have_posts() ) : while (have_posts()) : the_post();
However, if you’re doing this but not also moving one of your endwhiles to in front of the closing endif then it would cause problems. I would check your endwhile and your endif statements and make sure they’re all lining up with the if and while statements.
Thank you for your suggestion. Yes, I am moving all closing statements to match. I have re-written this code with different if’s and while’s in different positions and the only way I get any output is by having the items where they are now. I cannot sort out what I am doing that isn’t working.
Update: I have started to shift toward adding meta_query to my args and then put an if $count is greater than the query. My issue is that I can only get the $count to add up how many posts are in the CPT, instead of what matches the query. Have I gone the wrong way?
Here is the new code I am trying to make work:
$args = array(
'posts_per_page' => 500,
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' =>
array(
'relation' => 'AND',
array(
'key' => 'book_videos', // group
'compare' => 'EXISTS'
),
array(
'key' => 'book_videos_bk_videos', // group + nested repeater --> I want to add another array here that adds in a subfield of the nested repeater but it just doesn't work
'compare' => 'EXISTS'
),
),
'post_type' => 'books',
'post_status' => 'publish',
The way nested repeaters in a group field stores its subfield values is:
group_name_repeater_name_X_repeater_subfield
Where X is the repeater row and it increments starting at 0 so if you had a field called name in your book videos nested repeater, if any post had a value there, it would be at:
book_videos_bk_videos_0_name
That being said, you could adjust your meta_query to just query if that key exists, you don’t need to check if the group exists, as well. You’d just grab every post that has something in the first repeater spot for any required subfield value of the repeater. Does that make sense?
Thank you very much! Your explanation of how the values are stored was exactly what I needed. In all my research, I’d never seen an example of a nested inside group. I appreciate your help more than you know!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.