user3604744
user3604744

Reputation: 1

Wordpress WP_User_Query Pagination

I have a short snippet of code that queries the users of the website based on meta values. The code already works fine and perfect. Now, my only problem here is that I can't seem to figure out how to make the pagination work just like when you use pagination on worpdress posts. Your help means a lot to us. Thank you in advance and more power to stackoverflow Here's the code that needs to have a pagination:

<ul id="ulfriends">
<?php 
// The User Query
$user_query = new WP_User_Query( $args = array (
'number' => 10,
'meta_query'     => array(
    array(
    'key'       => 'sponsor',
    'value'     => $current_user->user_nicename,
    'compare'   => '=',
    'type'      => 'CHAR',
    ),
    ),
    'count_total'    => true,
) );
echo count($user_query->results);
// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
    echo '<li>' . $user->first_name . ' ' . $user->last_name . '<br>' . $user->date_activation . '<br>' . get_avatar( $user->user_email, 165 ) . '</li>' ;
    }
} else {
    echo 'No users found.';

} 
?>
</ul>

Upvotes: 0

Views: 6945

Answers (1)

Sandeep Sharma
Sandeep Sharma

Reputation: 506

try this code

<ul id="ulfriends">
 <?php 

 $no=12;// total no of author to display

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if($paged==1){
      $offset=0;  
    }else {
       $offset= ($paged-1)*$no;
    }


 $user_query = new WP_User_Query( array( 'role' => 'Subscriber',  'number' => $no, 'offset' => $offset ) );
       if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<li>' . $user->first_name . ' ' . $user->last_name . '<br>' . $user->date_activation . '<br>' . get_avatar( $user->user_email, 165 ) . '</li>' ;
    }
        } else {
            echo 'No users found.';
        } 
 ?>           
</ul>
<?php
            $total_user = $user_query->total_users;  
            $total_pages=ceil($total_user/$no);

              echo paginate_links(array(  
                  'base' => get_pagenum_link(1) . '%_%',  
                  'format' => '?paged=%#%',  
                  'current' => $paged,  
                  'total' => $total_pages,  
                  'prev_text' => 'Previous',  
                  'next_text' => 'Next',
                  'type'     => 'list',
                )); 
?>

Upvotes: 13

Related Questions