4

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>';
  • Why not just order by meta_value_num, why would you compare each one to 0? – Wyck Oct 29 '12 at 21:35
  • Doest work that way either but I need to leave out 0 scored and negatives – Pollux Khafra Oct 29 '12 at 22:31
  • Did you get the answer? – trusha Nov 18 at 6:59
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.