Support

Account

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

  • Hi @forbiddenchunk

    Absolutely no idea if this would work or not:

    
    <?php
    $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):
    	foreach($filter_price as $price):
    
    		$figure = new WP_Query( 
    			$args,
    			array( 
    				'key'     => 'price',
    				'value'   => $price,
    				'type' => 'numeric',
    				'compare' => '<=',
    			) 
    		);
    					
    		$query = new WP_Query( $figure );
    
    		$figure_total = $query->found_posts;
    
    		echo $figure_total;
    		
    	endforeach;
    endif; #endif $filter_price

    Basically, you loop all the individual prices and put them into an array
    Filter the prices to remove duplicates
    Then loop the filtered results and pass the value into your query
    Your query then returns the count

    Not tried the code!