Site icon Shyam Makwana

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

Exit mobile version