Support

Account

Home Forums Front-end Issues Sort by custom field and publish date Reply To: Sort by custom field and publish date

  • Thank you John, this solved my problem. I’m posting the updated code here for others to see. What i did is make the feature part of my meta_query and declare an index for “feature_clause” that just checks to see if the field exists. Then i added that to the “orderby” array including “date”, so both are sorted together. Here’s the updated query:

    $review_args = array ( 
    	'post_type' => 'customer_reviews',
    	'posts_per_page' => 20,
    	'post_status' => 'publish',
    
    	'meta_query' => array( 
    		'relation' => 'AND',
    		'feature_clause' => array(
    			'key' => 'featured',
    			'compare' => 'EXISTS',
    		),
    	),
    
    	'orderby' => array(
    		'featured' => 'DESC',
    		'date' => 'DESC',
    	),
    
    	'fields' => 'ids' 
    );