Last active
August 29, 2015 14:22
-
-
Save renekorss/1ed818edefd81af54666 to your computer and use it in GitHub Desktop.
Order Wordpress posts by multiple meta key values
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 | |
/** | |
* Wordpress order by multiple meta key values | |
* | |
* @author Rene Korss <[email protected]> | |
*/ | |
// Override orderby | |
function orderby_multiple_meta_keys( $orderby ){ | |
return str_replace( 'meta_value', 'meta_value, mt1.meta_value', $orderby ); | |
} | |
$args = array( | |
'meta_query' => array( | |
array( | |
'key' => 'first_meta_key', | |
'compare' => 'EXISTS' | |
), | |
array( | |
'key' => 'second_meta_key', | |
'compare' => 'EXISTS' | |
) | |
), | |
'orderby' => 'meta_value', | |
'order' => 'ASC' | |
); | |
// Replace orderby | |
add_filter( 'posts_orderby', 'orderby_multiple_meta_keys' ); | |
// Query | |
$the_query = new WP_Query( $args ); | |
// Remove our orderby | |
remove_filter( 'posts_orderby', 'orderby_multiple_meta_keys' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment