Quick patch : WP_User_Query orderby meta_value_num

This article is a quick patch/fix for orderby meta_value_num in WP_User_Query

When you want to sort users by their meta key there is option, but if meta value is number and you want to sort them, then there is nothing like meta_value_num in WordPress (at the time or writing (WordPress 4.1 )) for WP_User_Query. So here I have written a tiny code snippet that will be helpful.


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 ); 

Shyam has written 29 articles

Shyam is senior full stack developer, who loves to explore new technologies and work on them. He's passionate about coding so can code 24/7. He uses PHP as a backend programming language.

He knows Laravel, MySQL, AngularJS, ReactJS, Redis, Kubernetes, Git, CodeIgniter, PHP, MVC pattern, Lodash, jQuery, VanilaJS, Teamcity and many other technologies and tools.

Shyam writes notes and hacks on his blog (https://shyammakwana.me). In spare time he can be found @ StackOverflow or crafting any new open source application.

Passionate Programmer and Meditator #PERIOD.