Skip to content

Instantly share code, notes, and snippets.

@palicko
Created November 14, 2016 16:12
Show Gist options
  • Save palicko/3ad57429ef9ae196bebd244b235dc69f to your computer and use it in GitHub Desktop.
Save palicko/3ad57429ef9ae196bebd244b235dc69f to your computer and use it in GitHub Desktop.
Order by two meta values using meta_query
$meta_query = array(
'relation' => 'OR',
'featured' => array( // give the first meta key array an array key
'key' => 'is_featured',
'compare' => 'EXISTS',
'type' => 'NUMERIC'
),
'delivery_date' => array( // give the second meta key array an array key
'key' => 'to_be_delivered',
'compare' => 'EXISTS',
'type' => 'NUMERIC',
),
);
// order by using the meta array keys to reference them
// first order by 'delivery_date' value, then order by 'featured' value
$orderby = array(
'delivery_date' => 'DESC',
'featured' => 'DESC',
);
$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', $orderby );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment