I'm trying to get top 10 users in my wordpress blog based on user meta value.

I've tried few methods & finally able to get just top one user using below wpdb query. But clueless, how we can get top 10 users.

$top10alltime = $wpdb->get_row( "SELECT MAX( CAST( meta_value AS UNSIGNED ) ) AS top_user FROM $wpdb->usermeta WHERE meta_key = 'alltimeusers'" ); 

echo $top10alltime->top_user;

Is $wpdb is right for this purpose? Or is there any other solution to get this done? using WP_User_Query?

I've tried something with WP_User_Query.

foreach ( $users as $var => $order ) {
        'count_total' => false,
        'meta_key' => 'todayuserclicks',
        'orderby' => 'meta_value',
        'fields' => 'ID',
        'number' => 10,
        'order' => $order,

I can't get this done after this .. how can we do this?


I guess you can do with get_users, something like:

$topusers = get_users( array (
    'meta_key'     => 'todayuserclicks',
    'meta_value'   => '',
    'meta_compare' => '!=',
    'orderby'      => 'meta_value',
    'order'        => 'DESC',
    'number'       => '10',
) );
  • How can we get top 10 user ids & meta values with this? – Anvy Oct 27 '15 at 15:31
  • I assume you want to get the top 10 users with highest value of 'todayuserclicks', is that right? If so, this code will serve you, just do a loop of the $topusers after. You can also do a var_dump($topusers); to check what it returned. – Bruno Cantuaria Oct 27 '15 at 16:07
  • Yes, But it's not displaying in the descending order. I don't know which way it's showing. – Anvy Oct 27 '15 at 16:12

By using get_users() I meant something along the lines of the following:

$args = array(
  'meta_query' => array(
          'key'     => 'todayuserclicks',
          'compare' => 'EXISTS'
  'orderby'    => 'meta_value_num',
  'order'      => 'DESC',
  'number'     => '10',
  'fields'     => 'all_with_meta', // does NOT return meta, see below note
$todayuserclicks_top_10 = get_users( $args );


Even with the parameter fields set to all_with_meta no post meta values are included to the result. The documentation for WP_User_Query - Return Fields Parameter it says to this:

'all_with_meta' currently returns the same fields as 'all' which does not include user fields stored in wp_usermeta. You must create a second query to get the user meta fields by ID or use the __get PHP magic method to get the values of these fields.

  • I don't have any clue how can we return the meta value and user id's of top 10 users... It's returning an array and how can we get user details? Can you please help? – Anvy Oct 27 '15 at 15:15
  • Yes, when i've added 'alltimeusers' in the fields parameter, it's not giving me any alltimeusers meta values? It's giving me an array like this when i dump it array(7) { [0]=> string(1) "5" [1]=> string(1) "4" [2]=> string(1) "3" [3]=> string(1) "8" [4]=> string(1) "6" [5]=> string(1) "7" [6]=> string(1) "2" } – Anvy Oct 27 '15 at 15:31
  • I'm not getting all the data with all_with_meta too ... It's not showing info of my custom user meta field. – Anvy Oct 27 '15 at 15:41
  • Ohh .. Can I have some assistance on getting this? A quick code snippet? – Anvy Oct 27 '15 at 15:48
  • 1
    Got the solution, when the meta_value is numeric we should use 'meta_value_num' instead of meta_value. :) – Anvy Oct 28 '15 at 4:27

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.