Wp_User_Query not sorting by meta key

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

Related posts

Leave a Reply

1 comment

  1. you can try this code

     $args = array(
                'meta_query' => array(
                        array(
                            'key'     => 'score',
                            'value'   => 0,
                            'compare' => '>',
                            'type'    => 'numeric'
                        )
                ),
                '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>';
    

    For more information you can check this link
    https://codex.wordpress.org/Class_Reference/WP_User_Query