I’m trying to list users sorted by a meta value. I store a score for each user in their user meta which is just a number but the loop isn’t sorting. I include the score in the loop so that I can see that it’s not being sorted correctly. What am I doing wrong?
$args = array(
'meta_key' => 'score',
'meta_value' => 0,
'meta_compare' => '>',
'orderby' => 'meta_value_num',
'number' => 20
);
$suggested_user_query = new WP_User_Query( $args );
$users = $suggested_user_query->get_results();
echo '<div id="user_suggest">';
echo '<ul>';
foreach ($users as $user) {
// get all the user's data
$user_info = get_userdata($user->ID);
echo '<li>';
echo '<h3>'.bp_core_get_user_displayname( $user->ID ).'</h3>';
$score = get_user_meta( $user->ID, 'score', true );
echo $score;
echo '</li>';
}
echo '</ul>';
echo '</div>';
you can try this code
For more information you can check this link
https://codex.wordpress.org/Class_Reference/WP_User_Query