Top Authors list by highest post views with Ajax pagination

I'm trying to create an users list to sort all users by the highest post views for each user, I added $output = array_slice($topuser, 0, 10); to get 10 users when i click load more in the next page, but this is repeating the first 10 users only!

After searching i found is need to add $paged and $offset to get the next page for the next users!

But sorry, I don't know how :(

I'm highly appreciated your time and help.

here's all my code i have done.

authors-ranking-list.php

?php 
function MostPopularArtistsFullList() {

    global $wp_query;

    $topuser = array();

    $avatar_size = 100;

    $args = array(
        'role__in'     = array('contributor', 'author'),
        'hide_empty'     = '1'
     ); 
    $users = get_users( $args );

    foreach ( $users as $user ) {    

        $query = get_posts( array('author' = $user-ID, 'cat' = '3', 'numberposts' = -1, 'post_type'  = 'post' ));
        $counter = 0;

        $post_count = count_user_posts( $user-ID );

        if ( ! $post_count ) {
            continue;
        }

        // 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
        );
        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 10

    $rank=0;
    $rankpostcount=0;

    echo 'div id="top-artists-contributors"';

    foreach ($output as $user){

    $rank++;
    $rankpostcount++;

        $query = get_posts( array('author' = $user['id'], 'cat' = '3', 'numberposts' = -1, 'post_type'  = 'post' ));
        $avatar = get_avatar($user['id'], $avatar_size);
        $author_profile_url = get_author_posts_url($user['id']);
        $profile = get_userdata($user['id']);

        // update the rank for each user
        update_user_meta( $user['id'], 'user_rank', $rank );

        if (count($query)) {

        echo 'div class="rankpostcount-'.$rankpostcount.' single-item-9"';

        echo 'div class="members-name-9"a href="', $author_profile_url, '"' . $profile-first_name .'/adiv class="author-rank-9" title="Artist Rank"'.$rank.'/div/div';

        echo '/div';         
        }
    }

    echo '/div';

?
    div class="load_more_posts"style="text-align:center;margin-bottom: 30px; width:100%; float:left;"
        a id="load-more-rank-list" href="javascript:void(0)"Load more/a
            div class="loader" style="padding-top: 10px;"/div
        span class="no-more-post"/span
    /div  
?php

}
add_shortcode('top-artists-full-list', 'MostPopularArtistsFullList');
?

function.php

function loadMore() {

    global $wp_query;

    $no  = 9; 
    $page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 2;
    $offset = ($page-1)*$no;

    $topuser = array();

    $avatar_size = 100;

    $args = array(
        'role__in'     = array('contributor', 'author'),
        'hide_empty'     = '1'
     ); 
    $users = get_users( $args );

    foreach ( $users as $user ) {       

        $query = get_posts( array('author' = $user-ID, 'cat' = '3', 'numberposts' = -1, 'post_type'  = 'post' ));
        $counter = 0;

    $post_count = count_user_posts( $user-ID );

        if ( ! $post_count ) {
            continue;
        }

        // 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
        );
        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 10

    $rank=0;
    $rankpostcount=0;

    echo 'div id="top-artists-contributors"';

    foreach ($output as $user){

    $rank++;
    $rankpostcount++;

        $query = get_posts( array('author' = $user['id'], 'cat' = '3', 'numberposts' = -1, 'post_type'  = 'post' ));
        $avatar = get_avatar($user['id'], $avatar_size);
        $author_profile_url = get_author_posts_url($user['id']);
        $profile = get_userdata($user['id']);

        // update the rank for each user
        update_user_meta( $user['id'], 'user_rank', $rank );

        if (count($query)) {

        echo 'div class="rankpostcount-'.$rankpostcount.' single-item-9"';

        echo 'div class="members-name-9"a href="', $author_profile_url, '"' . $profile-first_name .'/adiv class="author-rank-9" title="Artist Rank"'.$rank.'/div/div';

        echo '/div';         
        }
    }

    echo '/div';

    die();
}

add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore');

load-more-users.js

jQuery(document).ready(function($) {

        jQuery("#load-more-rank-list").on("click",function(){ // When btn is pressed.
        jQuery("#load-more-rank-list").attr("disabled",true); // Disable the button, temp.
        load_posts();
        });
        var ppp = 10; // Post per page
        var pageNumber = 1; 

        function load_posts() {

        pageNumber++;
        var str = 'pageNumber=' + pageNumber + 'ppp=' + ppp + 'action=loadMore';
        $(".loader").html("img src='?php echo get_template_directory_uri(); ?/images/loading.gif'");
        jQuery.ajax({
            type: "POST",
            dataType: "html",
            url: "?php echo site_url(); ?/wp-admin/admin-ajax.php",
            data: str,
            success: function(data){
                var $data = $(data);
                if($data.length){
                    $("#top-artists-contributors").append($data);
                    $(".loader").html("");
                } else{
                    $("#load-more-rank-list").addClass('hide-more-btn');
                    $(".no-more-post").html("No More Post");
                    $(".loader").html("");

                }
            },
            error : function(jqXHR, textStatus, errorThrown) {
                $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
        return false;
        }
});

Topic list-authors ajax php pagination Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.