Support

Account

Home Forums Front-end Issues Count field selection in a WP Query Reply To: Count field selection in a WP Query

  • What about something like:

    <?php
    
    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):
    echo '<p>Price: '.$price.'</p>';
    
    	$args = array(
    		'post_type'         => 'cars',
    		'posts_per_page'	=> -1,
    		'meta_query'	=> array(
    			'relation'		=> 'AND',
    			array(
    				'key'		=> 'price',
    				'value'		=> $price,
    				'compare'	=> '<='
    			),
    		)
    	);
    	$wp_query = new WP_Query($args);
    
    	$figure_total = $wp_query->found_posts;
    	$count = count( $wp_query->get_posts() );	
    
    	#echo '<p>$figure_total: '.$figure_total.' count: '.$count.'</p>';
    	$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

    Again, untested code so may error or need some tweaking!