$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Top Authors list by highest post views with Ajax pagination|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Top Authors list by highest post views with Ajax pagination

matteradmin8PV0评论

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 .'</a><div 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 .'</a><div 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;
        }
});
Post a comment

comment list (0)

  1. No comments so far