Home › Forums › Front-end Issues › Get Total of Matching Fields in Repeater Within Custom Query
Hey everyone!
I’m setting up a site and have run into a wall. I have three custom post types: clients, flowers, hard_goods. The ACF set up is the following: Wedding Date (date picker), Flowers Count List (field name:’flowers_count_list’ – Repeater) that contains Flowers (field name: ‘flowers’ – relationship to cpt of flowers) and Flowers Count (field name: ‘flowers_count’ – number used per client). I have the archive page set up to display posts within a date range but I’m looking to get a total of the ‘flowers_count’ matching flowers by their name ‘flowers’ then a breakdown by wedding underneath that, all in alphabetical order.
Example:
Delilah: 12
Roses: 5
Tulips: 15
Here’s the code I currently have that’s showing the individual breakdown by client.
add_action('wp_ajax_myfilter', 'misha_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');
function misha_filter_function(){
$args = array(
'post_type' => 'clients',
'meta_key' => 'wedding_date',
'orderby' => 'meta_value',
'order' => $_POST['date'] // ASC or DESC
);
// for taxonomies / categories
if( isset( $_POST['categoryfilter'] ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['categoryfilter']
)
);
// create $args['meta_query'] array if one of the following fields is filled
if( isset( $_POST['start_date'] ) && $_POST['start_date'] || isset( $_POST['end_date'] ) && $_POST['end_date'] || isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
$args['meta_query'] = array( 'relation'=>'AND' ); // AND means that all conditions of meta_query should be true
// if both minimum price and maximum price are specified we will use BETWEEN comparison
if( isset( $_POST['start_date'] ) && $_POST['start_date'] && isset( $_POST['end_date'] ) && $_POST['end_date'] ) {
$args['meta_query'][] = array(
'key' => 'wedding_date',
'value' => array( $_POST['start_date'], $_POST['end_date'] ),
'type' => 'date',
'compare' => 'between'
);
} else {
// if only min price is set
if( isset( $_POST['start_date'] ) && $_POST['start_date'] )
$args['meta_query'][] = array(
'key' => 'wedding_date',
'value' => $_POST['start_date'],
'type' => 'date',
'compare' => '>'
);
// if only max price is set
if( isset( $_POST['end_date'] ) && $_POST['end_date'] )
$args['meta_query'][] = array(
'key' => 'wedding_date',
'value' => $_POST['end_date'],
'type' => 'date',
'compare' => '<'
);
}
// if post thumbnail is set
if( isset( $_POST['featured_image'] ) && $_POST['featured_image'] == 'on' )
$args['meta_query'][] = array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
);
// if you want to use multiple checkboxed, just duplicate the above 5 lines for each checkbox
$query = new WP_Query( $args );
if( $query->have_posts() ) :
echo '<div class="wedding-clients-gallery">';
while( $query->have_posts() ): $query->the_post();
echo '<div class="client-wedding"><a href="';
the_permalink();
echo '"><h2>';
the_title();
echo '</h2></a> date: ';
the_field('wedding_date');
// Check rows exists.
if( have_rows('flowers_count_list') ):
echo '<h3>Flowers List:</h3>';
// Loop through rows.
while( have_rows('flowers_count_list') ) : the_row();
$featured_posts = get_sub_field('flowers');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<li>
<?php
echo get_the_title( $post->ID );
echo '( ';
the_sub_field('flowers_count');
echo ' )';
?>
</li>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata();
endif;
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
// Check rows exists.
if( have_rows('hard_goods_count_list') ):
echo '<h3>Hard Goods List:</h3>';
// Loop through rows.
while( have_rows('hard_goods_count_list') ) : the_row();
$featured_posts = get_sub_field('hard_goods');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<li>
<?php
echo get_the_title( $post->ID );
echo '( ';
the_sub_field('hard_goods_count');
echo ' )';
?>
</li>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata();
endif;
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
echo '</div>';
endwhile;
echo '</div>';
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
Thank you for reading through this and the potential help!
There isn’t any way to do a query in WP using WP query functions that will allow you to get this total.
You would need to query all posts, loop over those posts, loop over the repeater for each post, keep a running total of all of the values.
Thank you for pointing me in the right direction! I was able to add those into an array, although within an extra associate array, with the below and just need to figure out how to add the similar indices, and add the counts and details.
if( have_rows('flowers_count_list') ):
// Loop through rows.
while( have_rows('flowers_count_list') ) : the_row();
$featured_posts = get_sub_field('flowers');
if( $featured_posts ): ?>
<?php foreach( $featured_posts as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<?php /*
echo get_the_title( $post->ID );
echo '( ';
the_sub_field('flowers_count');
echo ' )';
*/
$flowers_count_list_full_array[] = [
'name'=> get_the_title( $post->ID ),
'count'=>get_sub_field('flowers_count'),
'details'=> get_the_title().' ('.get_sub_field('flowers_count').')',
];
?>
<?php endforeach; ?>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata();
endif;
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
// Check rows exists.
if( have_rows('hard_goods_count_list') ):
//echo '<h3>Hard Goods List:</h3>';
// Loop through rows.
while( have_rows('hard_goods_count_list') ) : the_row();
$featured_posts = get_sub_field('hard_goods');
if( $featured_posts ): /*?>
<ul>
<?php foreach( $featured_posts as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<li>
<?php
echo get_the_title( $post->ID );
echo '( ';
the_sub_field('hard_goods_count');
echo ' )';
?>
</li>
<?php endforeach; ?>
</ul>
<?php*/
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata();
endif;
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
echo '</div>';
endwhile;
echo '</div>';
echo '<h3>Flowers List:</h3>';
echo 'Filled Array!: <br/>';
print_r($flowers_count_list_full_array);
echo '<br/>Combined Array!: <br/>';
$combined_flowers_count_list_full_array = $flowers_count_list_full_array;
var_dump(array_merge($flowers_count_list_full_array));
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
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.