Created
July 9, 2016 05:27
-
-
Save stevenspads/376fae003a401fcc34cd95bfec358fbd to your computer and use it in GitHub Desktop.
Ordering WP_QUERY results by several meta keys in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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