List users with the most total posts view

List authors by Total Post Views –

If view is custom field name then this functions will return all users in descending order as per their post’s TOTAL view count.

  • Just drop in this function in your theme’s functions.php file .
  • Call function wpse_61194_top_authors('view') with meta_key name.
// Usage - 
//      $list = wpse_61194_top_authors('view');
//      foreach ($list as $li) {
//      echo '<li>'.$li->user_nicename.' - '.$li->view.'</li>';
//  }   

function wpse_61194_top_authors ($meta_key) {

    global $wpdb;

    $query = "SELECT wp.post_author, wu.user_nicename, SUM(wm.meta_value) as $meta_key
    FROM $wpdb->posts wp, $wpdb->postmeta wm, $wpdb->users wu
    WHERE wp.ID = wm.post_id 
    AND wp.post_author = wu.id
    AND wm.meta_key = '$meta_key' 
    AND wp.post_type="post"
    GROUP BY wp.post_author
    ORDER BY $meta_key DESC";

    $post_count = $wpdb->get_results($query);
    return $post_count;
}

Leave a Comment