Home › Forums › Add-ons › Repeater Field › Querying all repeater fields on a page
I was wondering how do I show all repeater fields within the query? Currently I am assigning file downloads to products. The repeater is “brochure” with sub fields of “brochure-name”, “brochure-file”.
Eg the following will display
Example Category
Example Product 1
Example Product 1
Though I would like to also show:
Example Category 1
Example Product 1
-Brochure 1
-Brochure 2
Example Product 2
-Brochure 1
Example Category 2
Example Product 3
-Brochure 1
-Brochure 2
Example Product 4
-Brochure 1
This is the code I am using to output above without the repeater field links:
<?php
$custom_terms = get_terms('range');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'range',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br />';
endwhile;
}
}
?>
This is the code I am using on the products page to show it:
<?php if( have_rows('brochure') ): ?>
<ul>
<?php while( have_rows('brochure') ): the_row();
// vars
$name = get_sub_field('brochure-name');
$file = get_sub_field('brochure-file');
?>
<li>
<?php if( $file ): ?>
<a href="<?php echo $file; ?>">
<?php endif; ?>
<?php echo $name; ?>
<?php if( $file ): ?>
</a>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
You need to put your second snippet of code inside the while loop of the first snippet of code.
Okay managed to do it – but is there a way I can just output the Title + Files only if the range has products with files uploaded?
Have the following – but it will still display Range (category/taxonomy) title even if non of the products in it have a brochure uploaded to them.
<?php
$custom_terms = get_terms('range');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'range',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
if( have_rows('brochure') ):
echo '<h6><a href="'.get_permalink().'">'.get_the_title().'</a></h6>';
echo '<ul>';
while( have_rows('brochure') ): the_row();
$name = get_sub_field('brochure-name');
$file = get_sub_field('brochure-file');
echo '<li>';
if( $file ):
echo '<a href="'.$file.'">';
endif;
echo $name;
if( $file ):
echo '</a>';
endif;
echo '</li>';
endwhile;
echo '</ul>';
endif;
endwhile;
}
}
?>
There isn’t any good way to query posts based on a repeater field. Basically what you need to do is a MySQL query using $wpdb to get a list of post IDs that have the values you want and then use that list with the “post__in” argument for WP_Query. Doing the MySQL query is covered here: http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/
The topic ‘Querying all repeater fields on a page’ is closed to new replies.
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.