Skip to content

Instantly share code, notes, and snippets.

@wpmark
Created November 18, 2015 12:48
Show Gist options
  • Save wpmark/217422b4210fdd8e3c46 to your computer and use it in GitHub Desktop.
Save wpmark/217422b4210fdd8e3c46 to your computer and use it in GitHub Desktop.
WP_Query Ordered By Multiple Meta Keys
<?php
/* build a new wp_query */
$classes = new WP_Query(
array(
'post_type' => 'wpmark_class_time', // post type to query
'posts_per_page' => -1, // get all the posts not limited
'meta_query' => array(
'relation' => 'AND',
'day' => array( // give the first meta key array an array key
'key' => '_wpmark_day',
'compare' => 'EXISTS',
'type' => 'NUMERIC'
),
'location' => array(
'key' => '_wpmark_location',
'value' => $post->ID,
'compare' => 'EXISTS'
),
'start_time' => array(
'key' => '_wpmark_start_time',
'compare' => 'EXISTS',
'type' => 'NUMERIC',
)
),
// order by using the meta array keys to reference them
'orderby' => array(
'day' => 'ASC',
'start_time' => 'ASC'
),
'fields' => 'ids'
)
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment