I'm asked by my friend to create a widget for his multiauthor blog. He wants to list 3 authors with the most total post view from their posts. I've got the job done for the post view using post metadata. But I'm confused on querying the authors with that criteria. Any idea?

Thanks for the help.

[update] I'm using this codes and its work. Is there any simpler version?

    global $wpdb;
    $topuser = array();

    // query all posts by each user
    $users = $wpdb->get_results("SELECT ID FROM $wpdb->users ORDER BY ID"); 
    foreach ( $users as $user ) {       
        $query = get_posts( array('author' => $user->ID ));
        $counter = 0;

        // get each post of a user
        foreach ( $query as $post ){
            $views = absint( get_post_meta( $post->ID, 'post_views_count', true ) );
            $counter += $views;
        $topuser[] = array( 'id' => $user->ID, 'views' => $counter);

    // function to sort array based on views count
    function sortViews($a, $b) {
        return $b['views'] - $a['views'];
    usort($topuser, 'sortViews'); // sort the array
    $output = array_slice($topuser, 0, 3); // slice the array by limit 3

    // output the result: user, total postview count, latest post
    foreach ($output as $user){
        $profile = get_userdata($user['id']);
        $query = get_posts( array('author' => $user['id'], 'numberposts' => 1 ));

        echo '<p>' . $profile->user_nicename .' ('. $user['views'] .')</p>';            
        foreach ( $query as $post ){
            echo '<a href="' . $post->post_name . '">' . $post->post_title . '</a>';
            echo get_post_meta($post->ID, 'post_views_count', true);
  • Each post has the post_author field with the id of the user that wrote the post. Is this what you're looking for? Aug 8, 2012 at 10:39
  • Can you post an example of the code you have already?
    – Vince P
    Aug 8, 2012 at 11:07
  • @JordiCabot yeah, I know that. What what i'm looking for is to get 3 users which has the most pageview sum.
    – rizqyhi
    Aug 9, 2012 at 7:07

2 Answers 2


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;
  • +1 sql made simpler, but I've found the way :)
    – rizqyhi
    Aug 9, 2012 at 7:50
  • yes, This single query return's multi array of user + total count in sorted order.
    – Amit Kosti
    Aug 9, 2012 at 7:57

There is no default wordpress api that will give you list of all users.. So you have to manually write query for user table using wp_query

First query all users and then for each users find theirs posts and sum up total view, compare all total views and find the greater three..

Alternatively, you can store total_view field with user meta table for each user and increase it when the post is viewed, in that way you only need to query user table to find user with highest total_view field.

  • I'm working with this logic yesterday and its work. Thanks :)
    – rizqyhi
    Aug 9, 2012 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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