Pagination for user list

I am showing all the users by using the get_user() function. But I need to paginate the results. I tried a lot, but didn't succeed. Here is the code, which doesn't work as expected:

$args = array( 'meta_query' = array(
    array(
        'key'   = 'ib_s2member_custom_fields',
        'value' = trim($_GET["country"]),
        'compare' = 'LIKE'
    ),
array(
        'key'   = 'ib_s2member_custom_fields',
        'value' = trim($_GET["city"]),
        'compare' = 'LIKE'
    ),
    array(
        'key'   = 'ib_s2member_custom_fields',
        'value' = $first_name,
        'compare' = 'LIKE'
    ),
array(
        'key'   = 'ib_s2member_custom_fields',
        'value' = $last_name,
        'compare' = 'LIKE'
    )
) );

$users = get_users( $args );

Topic pagination users Wordpress

Category Web


You can use WP_User_Query class instead of get_users() function to achieve this. The new modified code will similar to following code.

<?php
/*
 * We start by doing a query to retrieve all users
 * We need a total user count so that we can calculate how many pages there are
 */

$count_args  = array(
'number'    => 999999      
);
$user_count_query = new WP_User_Query($count_args);
$user_count = $user_count_query->get_results();

// count the number of users found in the query
$total_users = $user_count ? count($user_count) : 1;

// grab the current page number and set to 1 if no page number is set
$page = isset($_GET['p']) ? $_GET['p'] : 1;

// how many users to show per page
$users_per_page = 5;

// calculate the total number of pages.
$total_pages = 1;
$offset = $users_per_page * ($page - 1);
$total_pages = ceil($total_users / $users_per_page);

// main user query
$args = array(
'number'    => $users_per_page,
'offset'    => $offset, 
'meta_query' => array(
'relation' => 'OR',
array(
    'key'     => 'country',
    'value'   => 'Israel',
    'compare' => '='
),
array(
    'key'     => 'age',
    'value'   => array( 20, 30 ),
    'type'    => 'numeric',
    'compare' => 'BETWEEN'
)
)
);

$user_query = new WP_User_Query( $args );

// Get the results
$users = $user_query->get_results();

// User Loop
if ( ! empty( $users->results ) ) {
foreach ( $users->results as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}

// grab the current query parameters
$query_string = $_SERVER['QUERY_STRING'];

// The $base variable stores the complete URL to our page, including the current page arg

// if in the admin, your base should be the admin URL + your page
$base = admin_url('your-page-path') . '?' . remove_query_arg('p', $query_string) . '%_%';

// if on the front end, your base is the current page
//$base = get_permalink( get_the_ID() ) . '?' . remove_query_arg('p', $query_string) . '%_%';

echo paginate_links( array(
'base' => $base, // the base URL, including query arg
'format' => '&p=%#%', // this defines the query parameter that will be used, in this case "p"
'prev_text' => __('&laquo; Previous'), // text for previous page
'next_text' => __('Next &raquo;'), // text for next page
'total' => $total_pages, // the total number of pages we have
'current' => $page, // the current page
'end_size' => 1,
'mid_size' => 5,
));
?>

For more information on WP_User_Query visit this page.

About

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