I think I’m nearly there with this, but I can’t get the pagination links to show for a directory of authors I’m creating.
My code is below, but I don’t know how to get the links to navigate between pages of authors to work. Can anyone help me? I’ve got the feeling this might be of use, but I don’t know how to implement it:
Thanks
Osu
<?php
/* ****************************************************************** */
/* !LIST AUTHORS */
/* ****************************************************************** */
// THANKS TO:
// http://www.mattvarone.com/wordpress/list-users-with-wp_user_query/
// pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Needed for pagination
$paged -= 1;
$limit = 2;
$offset = $paged * $limit;
// prepare arguments
$args = array(
// search only for Authors role
'role' => 'Subscriber',
// order results by display_name
'orderby' => 'display_name',
// return all fields
'fields' => 'all_with_meta',
'number' => $limit,
'offset' => $offset
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($args);
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if (!empty($authors))
{
echo '<div class="author-entry">';
// loop trough each author
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID); ?>
<span style="float:left;padding:0 5px 0 0;"><?php echo get_avatar( $author->ID, 50 ); /* http://codex.wordpress.org/Function_Reference/get_avatar */ ?></span>
<span class="fn"><strong>First name</strong> : <?php echo $author_info->first_name; ?></span><br />
<span class="ln"><strong>Last name</strong> : <?php echo $author_info->last_name; ?></span><br />
<span class="em"><strong>Email address</strong> : <a href="mailto:<?php echo $author_info->user_email; ?>"><?php echo $author_info->user_email; ?></a></span><br />
<span class="we"><strong>Website</strong> : <a href="<?php echo $author_info->user_url; ?>"><?php echo $author_info->user_url; ?></a></span><br />
<span class="de"><strong>Bio</strong> :<br /><?php echo $author_info->description ; ?></span>
<div class="clear"> </div>
<?php
}
echo '</div>';
} else {
echo 'No authors found';
}
?>
<?php /* WHAT DO I PUT HERE TO CREATE THE PAGINATION LINKS? */ ?>
This should get you really close. I haven’t tested it, but it’s nearly identical to a setup I’ve used a few times.
You really should not use the answer by Pippin. The query is very inefficient.
$user_count_query
in the example can return up to 999,999 users from your database to your script, with all of the user fields. This will surely hit memory and/or time limits for PHP if/when your site grows large enough.But that may have been the only solution back in 2012.
Here is a better way to do it. In this example I’ve only got next and previous page but if you do need numbered pagination, the variables are there to build it out. WordPress doesn’t have a pagination function that is compatible with WP_User_Query (to my knowledge).
Example showing page 2:
Update 6/8/2018: How to add page numbers instead of Next/Previous
If you want to have page numbers instead of next/previous page links, here is how you can set that up. Note that you will need to replace the numbers with page links, they will not be clickable in this example (based on https://stackoverflow.com/a/11274294/470480, modified to show a consistent amount of middle numbers and not add the “…” unless a page is actually skipped).
You can also see my gist file which contains a reusable function for this purpose.
Output (from page 1 to 10):
Full credit should go to @radley-sustaire for his answer, but I spotted a small glitch with it so I’m sharing my version of the answer here.
With my version I was also filtering results by location, keyword etc so some pages had less results than the ‘$users_per_page’ var. So for example if my users per page was set to show 10, but the results of the filter only returned 3 users, I got ‘Displaying 10 of 3 users’ at the top of the page. Obviously this didn’t make sense so I added a simple “if” statement to check if the results count was higher than the ‘$users_per_page’ variable.
Radley, if you edit your answer with the update, I will happily vote for it as the correct answer as I think it is better than Pippin’s solution.
So this is the final code for anyone that wants it.