Skip to content

Instantly share code, notes, and snippets.

@technoknol
Created February 3, 2015 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technoknol/34030628b7b2e71c26a0 to your computer and use it in GitHub Desktop.
Save technoknol/34030628b7b2e71c26a0 to your computer and use it in GitHub Desktop.
Patch : WP_User_Query orderby meta_value_num
<?php
// Paste this code in functions.php of your active theme.
add_action( 'pre_user_query', 'wps_pre_user_query' );
function wps_pre_user_query( &$query ) {
global $wpdb;
if ( isset( $query->query_vars['orderby'] ) && 'meta_value_num' == $query->query_vars['orderby'] )
$query->query_orderby = str_replace( 'user_login', "$wpdb->usermeta.meta_value+0", $query->query_orderby );
}
// Usage ::
$args['order'] = 'ASC';
$args['orderby'] = 'meta_value_num' ;
$args['meta_key'] = 'users_numeric_meta_value' ;
$user_query = new WP_User_Query( $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment