最新消息: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 - How to create pagination for users list using custom array?

matteradmin3PV0评论

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


    }

}

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


    }

}
Share Improve this question edited Apr 5, 2019 at 10:58 Adham Mohamed asked Mar 23, 2019 at 8:14 Adham MohamedAdham Mohamed 1853 silver badges16 bronze badges 18
  • 1 This may work as guide to resolve your issue. – Qaisar Feroz 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. – Qaisar Feroz 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. – Adham Mohamed Commented Mar 23, 2019 at 17:16
  • 1 Try replacing $rank=0; with ` $rank=$page * $limit;` – Qaisar Feroz Commented Mar 23, 2019 at 17:32
  • 1 Set $rank=$offset; and $rankpostcount=$offset; – Qaisar Feroz Commented Mar 23, 2019 at 17:46
 |  Show 13 more comments

1 Answer 1

Reset to default 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;
    ?>
Post a comment

comment list (0)

  1. No comments so far