Created
August 15, 2012 17:40
-
-
Save wpsmith/3361867 to your computer and use it in GitHub Desktop.
Orderby meta_value last_name via pre_user_query in WP_User_Query
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 | |
// prepare arguments | |
$args = array( | |
'meta_key' => 'last_name', | |
'query_id' => 'wps_last_name', | |
); | |
// Create the WP_User_Query object | |
$author_query = new WP_User_Query( $args ); |
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 | |
add_action( 'pre_user_query', 'wps_pre_user_query' ); | |
/* | |
* Modify the WP_User_Query appropriately | |
* | |
* Checks for the proper query to modify and changes the default user_login for $wpdb->usermeta.meta_value | |
* | |
* @param WP_User_Query Object $query User Query object before query is executed | |
*/ | |
function wps_pre_user_query( &$query ) { | |
global $wpdb; | |
if ( isset( $query->query_vars['query_id'] ) && 'wps_last_name' == $query->query_vars['query_id'] ) | |
$query->query_orderby = str_replace( 'user_login', "$wpdb->usermeta.meta_value", $query->query_orderby ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment