0

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

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.

| improve this answer | |
  • I need pagination code. My code is working well. – user2311322 Aug 26 '13 at 8:16
  • @krish You have to use WP_User_Query class as above instead of get_users() function to make the pagination work. – Vinod Dalvi Aug 26 '13 at 8:22
  • i used WP_User_Query class but result is not showing.what to do sir?? – user2311322 Aug 26 '13 at 13:37
  • I have changed it a little bit. Please recheck it and let me know is it working or not? – Vinod Dalvi Aug 26 '13 at 16:27
  • Hi Vinod, If i use WP_User_Query class , no data is displaying except pagination no . i dont think WP_User_Query class will work in my query. any possibilities? – user2311322 Aug 27 '13 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.