Support

Account

Home Forums Front-end Issues Crazy complex Query with sorting and filtering. Totally stumped. Reply To: Crazy complex Query with sorting and filtering. Totally stumped.

  • Here’s what I ended up with, and it seems to work.

    
    $upcoming_query = new WP_Query(array(
    	'post_type'		=> 'my_event',
    	'numberposts'		=> -1,
    	'posts_per_page'	=> 1000000,
    
    	'meta_query' => array(
    		'relation' => 'AND',
    		'start_date_clause' => array(
    			'key' 		=> 'start_date',
    			'value' 	=> Carbon::now()->format("Ymd"),
    			'compare' 	=> '>=',
    		),
    		'same_date_sort_clause' => array(
    			'key' => 'same_date_sort', // higher number is earlier
    			'compare' => 'EXISTS',
    		), 
    	),
    
    	'orderby' => array(
    		'start_date_clause' 		=> "DESC",
    		'same_date_sort_clause' 	=> "ASC"
    	)
    	
    ));
    

    The same_date_sort/EXISTS clause doesn’t do anything (they all have that field), but it lets me use same_date_sort_clause/ASC for sorting below.