0

After searching I didn't found any solution, So I'm wondering how can I create pagination for users list like that?

I'm highly appreciated your help, Thanks.

<?php 

    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

    $output = $topuser; // all users

    $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>';
}

?>

Update 1

This is what I have tried to do but i can't access to the second page!

<?php 

    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

    $page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
    $total = count( $topuser ); //total items in array    
    $limit = 20; //per page    
    $totalPages = ceil( $total/ $limit ); //calculate total pages
    $page = max($page, 1); //get 1 page when $_GET['page'] <= 0
    $page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
    $offset = ($page - 1) * $limit;
    if( $offset < 0 ) $offset = 0;

    $topuser = array_slice( $topuser, $offset, $limit );

    $rank=0;
    $rankpostcount=0;

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

    foreach ($topuser 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>';
}

$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';   
if( $totalPages != 0 ) 
{
  if( $page == 1 ) 
  { 
    $pagerContainer .= ''; 
  } 
  else 
  { 
    $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> &#171; prev page</a>', $page - 1 ); 
  }
  $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>'; 
  if( $page == $totalPages ) 
  { 
    $pagerContainer .= ''; 
  }
  else 
  { 
    $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> next page &#187; </a>', $page + 1 ); 
  }           
}                   
$pagerContainer .= '</div>';

echo $pagerContainer;
?>

Update 2

Trying to update users ranks meta with ajax

function update_users_ranks() {

    global $wp_query;

    $topuser = array();

$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 = $topuser;
    $rank=0;

    // Update ranks
    foreach ($output as $user){
       $rank++;
       update_user_meta( $user['id'], 'user_rank', $rank );
    }
    die();
}
add_action('wp_ajax_update_users_ranks' , 'update_users_ranks');

Update 3

Here how I set and get and updating the post views.

// function to display number of posts.
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
        }
        if ($count > 1000) {
        return round ( $count / 1000 ,1 ).'K';
    }
    return $count.' ';

}

// function to count views.
function setPostViews($postID) {

    $user_ip = $_SERVER['REMOTE_ADDR']; //retrieve the current IP address of the visitor
    $key = $user_ip . 'x' . $postID; //combine post ID & IP to form unique key
    $value = array($user_ip, $postID); // store post ID & IP as separate values (see note)
    $visited = get_transient($key); //get transient and store in variable

    //check to see if the Post ID/IP ($key) address is currently stored as a transient
    if ( current_user_can('administrator') || false === ( $visited ) ) {

        //store the unique key, Post ID & IP address for 12 hours if it does not exist
        set_transient( $key, $value, 60*60*1 );

        // now run post views function
        $count_key = 'post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if($count==''){
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
        }else{
            $count++;
            update_post_meta($postID, $count_key, $count);
        }


    }

}
18
  • 1
    This may work as guide to resolve your issue. Commented Mar 23, 2019 at 12:38
  • 1
    Change $page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1; and $link = get_the_permalink().'?page=%d'; to $page = ! empty( $_GET['pg'] ) ? (int) $_GET['pg'] : 1; and $link = get_the_permalink().'?pg=%d'; I mean avoid using page in ` $_GET []` as it creates a conflict with pretty links used by WP. Commented Mar 23, 2019 at 17:12
  • 1
    OMG! Working :), Thanks so much, You make my day, Please add it as answer so i can accept it, Thank you again. Commented Mar 23, 2019 at 17:16
  • 1
    Try replacing $rank=0; with ` $rank=$page * $limit;` Commented Mar 23, 2019 at 17:32
  • 1
    Set $rank=$offset; and $rankpostcount=$offset; Commented Mar 23, 2019 at 17:46

1 Answer 1

2
<?php 

        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





        $rank = 0;

        // Update rank of each user before pagination
        foreach ($topuser as $user){
           $rank++;
           update_user_meta( $user['id'], 'user_rank', $rank );
        }



        // Pagination 
        $page = ! empty( $_GET['pg'] ) ? (int) $_GET['pg'] : 1;
        $total = count( $topuser ); //total items in array    
        $limit = 20; //per page    
        $totalPages = ceil( $total/ $limit ); //calculate total pages
        $page = max($page, 1); //get 1 page when $_GET['pg'] <= 0
        $page = min($page, $totalPages); //get last page when $_GET['pg'] > $totalPages
        $offset = ($page - 1) * $limit;
        if( $offset < 0 ) $offset = 0;



        // Get users from array for current page
        $topuser = array_slice( $topuser, $offset, $limit );

        //$rank=$offset;
        $rankpostcount=$offset;

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

        foreach ($topuser 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']);

            // get updated rank for each user
            $rank = get_user_meta( $user['id'], 'user_rank', true );

            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>';
    }

    $link = get_the_permalink().'?pg=%d';
    $pagerContainer = '<div style="width: 300px;">';   
    if( $totalPages != 0 ) 
    {
      if( $page == 1 ) 
      { 
        $pagerContainer .= ''; 
      } 
      else 
      { 
        $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> &#171; prev page</a>', $page - 1 ); 
      }
      $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>'; 
      if( $page == $totalPages ) 
      { 
        $pagerContainer .= ''; 
      }
      else 
      { 
        $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> next page &#187; </a>', $page + 1 ); 
      }           
    }                   
    $pagerContainer .= '</div>';

    echo $pagerContainer;
    ?>
13
  • Hello brother @QaisarFeroz, I don't mean to bother you but i had another small issue with ranking, Now when i visit this page it should be calculate the rank for all users but not working! every time i need to load the other pages for updating the rank meta of each user :( , there are any way to update the rank meta for all users without loading the pages for updating? Thanks! Commented Apr 4, 2019 at 19:32
  • 1
    @AdhamMohamed. I have updated the answer. Now user's ranks are updated before pagination. Commented Apr 5, 2019 at 6:02
  • You are amazing +1, Thanks so much :) Commented Apr 5, 2019 at 7:25
  • Just a question, is there a way to update the rank meta with ajax without visiting this page to update the ranking for all users? I don't need take much time from you, just give me a hint and I will search about to save your time, Thanks again :) Commented Apr 5, 2019 at 7:30
  • 1
    Remove die(); from function update_users_ranks() and add_action('wp_ajax_update_users_ranks' , 'update_users_ranks'); because we are not using ajax. Commented Apr 5, 2019 at 11:09

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.