Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevenspads/376fae003a401fcc34cd95bfec358fbd to your computer and use it in GitHub Desktop.
Save stevenspads/376fae003a401fcc34cd95bfec358fbd to your computer and use it in GitHub Desktop.
Ordering WP_QUERY results by several meta keys in WordPress
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'games',
'author' => $user_id,
'meta_query' => array(
'relation' => 'AND',
'game_date' => array(
'key' => 'game_date',
'compare' => 'EXISTS',
),
'game_time' => array(
'key' => 'game_time',
'compare' => 'EXISTS',
)
),
'orderby' => array(
'game_date' => 'DESC',
'game_time' => 'ASC',
),
'posts_per_page' => NUMBER_PER_PAGE,
'paged' => $paged
);
$listings = new WP_Query( $args );
if ( $listings->have_posts() )
{
while ( $listings->have_posts() ) : $listings->the_post();
//...
endwhile;
}
else
{
//...
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment