Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

I originally was going to post a question on how I would go about ranking a list of authors by the amount of post views from all of their posts combined. But I luckily found a solution here: List users with the most total posts viewList users with the most total posts view

This is the code I'm using from that page, except I tweaked it a little to fetch post views from a plugin I use:

<?php 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,  'posts_per_page' => '10000000000'));
    $counter = 0;

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

// 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, 10); // 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, get_post_views('normal'), true);
    }
} ?>

Now I would like to take this solution a step further towards my vision when first coming up with the idea.

I want to show the specific rank (position in the list) of each user on their buddypress profile page. I would paste this code in member-header.php

Example: It would say something like "User Rank: 15", meaning that this user shows up as the 15th result when executing the above query (of course I would change the number from 10 to more which is why that would be possible).

Hope you guys can help out. Thanks in advance :)

I originally was going to post a question on how I would go about ranking a list of authors by the amount of post views from all of their posts combined. But I luckily found a solution here: List users with the most total posts view

This is the code I'm using from that page, except I tweaked it a little to fetch post views from a plugin I use:

<?php 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,  'posts_per_page' => '10000000000'));
    $counter = 0;

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

// 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, 10); // 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, get_post_views('normal'), true);
    }
} ?>

Now I would like to take this solution a step further towards my vision when first coming up with the idea.

I want to show the specific rank (position in the list) of each user on their buddypress profile page. I would paste this code in member-header.php

Example: It would say something like "User Rank: 15", meaning that this user shows up as the 15th result when executing the above query (of course I would change the number from 10 to more which is why that would be possible).

Hope you guys can help out. Thanks in advance :)

I originally was going to post a question on how I would go about ranking a list of authors by the amount of post views from all of their posts combined. But I luckily found a solution here: List users with the most total posts view

This is the code I'm using from that page, except I tweaked it a little to fetch post views from a plugin I use:

<?php 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,  'posts_per_page' => '10000000000'));
    $counter = 0;

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

// 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, 10); // 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, get_post_views('normal'), true);
    }
} ?>

Now I would like to take this solution a step further towards my vision when first coming up with the idea.

I want to show the specific rank (position in the list) of each user on their buddypress profile page. I would paste this code in member-header.php

Example: It would say something like "User Rank: 15", meaning that this user shows up as the 15th result when executing the above query (of course I would change the number from 10 to more which is why that would be possible).

Hope you guys can help out. Thanks in advance :)

Source Link

Show individual author rank from query of cumulative post view count for all authors

I originally was going to post a question on how I would go about ranking a list of authors by the amount of post views from all of their posts combined. But I luckily found a solution here: List users with the most total posts view

This is the code I'm using from that page, except I tweaked it a little to fetch post views from a plugin I use:

<?php 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,  'posts_per_page' => '10000000000'));
    $counter = 0;

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

// 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, 10); // 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, get_post_views('normal'), true);
    }
} ?>

Now I would like to take this solution a step further towards my vision when first coming up with the idea.

I want to show the specific rank (position in the list) of each user on their buddypress profile page. I would paste this code in member-header.php

Example: It would say something like "User Rank: 15", meaning that this user shows up as the 15th result when executing the above query (of course I would change the number from 10 to more which is why that would be possible).

Hope you guys can help out. Thanks in advance :)