Home › Forums › Front-end Issues › Count field selection in a WP Query › Reply To: Count field selection in a WP Query
Hi @jarvis,
It worked, but not 100% but looking better thanks 🙂
This is the code which I updated slightly;
$args = array(
'post_type' => 'cars',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'post_type' => 'cars',
'post_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'sold',
'value' => array('no'),
'compare' => 'IN',
),
)
)
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) :
$get_price = array();
while ($wp_query->have_posts()) : $wp_query->the_post();
$get_price[] = get_field('price');
endwhile;
endif; #endif $wp_query
$filter_price = array_unique($get_price);
if($filter_price):
echo '<select name="sort_price" id="sort_price">';
echo '<option value="" selected disabled>Select...</option>';
echo '<option value="">All</option>';
foreach($filter_price as $price):
$args = array(
'post_type' => 'cars',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'price',
'value' => $price,
'compare' => '<='
),
array(
'key' => 'sold',
'value' => array('no'),
'compare' => 'IN',
)
)
);
$wp_query = new WP_Query($args);
$figure_total = $wp_query->found_posts;
$count = count( $wp_query->get_posts() );
$round_price = round($price, -3);;
echo '<option value="'.$round_price.'">Up to £'.$round_price.' ('.$count.')</option>';
wp_reset_query();
endforeach;
echo '</select>';
endif; #endif $filter_price
It outputs the fields but seems to have duplicated them – so the options are;
<option value="5000">Up to £5000 (4)</option>
<option value="7000">Up to £7000 (6)</option>
<option value="7000">Up to £7000 (8)</option>
<option value="8000">Up to £8000 (9)</option>
<option value="10000">Up to £10000 (12)</option>
<option value="10000">Up to £10000 (1)</option>
<option value="9000">Up to £9000 (11)</option>
<option value="6000">Up to £6000 (5)</option>
<option value="8000">Up to £8000 (10)</option>
<option value="7000">Up to £7000 (7)</option>
<option value="4000">Up to £4000 (2)</option>
<option value="5000">Up to £5000 (3)</option>
As you can see it duplicates them and some of the counts aren’t right as
Upto £40000 (0) [cars in that price range]
Upto £50000 (2) [cars in that price range]
Upto £50000 (3) [cars in that price range]
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.