WordPress: get_users by post count but display alphabetically by first name

I am display a list of all site (except subscribers) users with get_users, but limiting the out to 15 (then static link to all authors). i am sorting by display name, so they come out in alphabetical order by first name. but if there are 40 users, the first 15 will be, say, A-L, but always leave off M-Z.

So I can order by post count for a random list of 15, but how to I then alphabetize that list?

<?php
// Get all users order by amount of posts
$allUsers = get_users('orderby=display_name&number=15');
$users = array();
// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser) {
if(!in_array( 'subscriber', $currentUser->roles )) {
$users[] = $currentUser;
}
}
?>

<h3 class="text-uppercase">
Contributors
</h3>

<div class="contributor-names">
<?php foreach($users as $user) { ?>
<span>
<a href="<?php echo get_author_posts_url( $user->ID ); ?>" title="Read Articles" class="black-link">
<?php echo $user->display_name; ?>
</a>
</span>
<?php } ?>

<a href="/colophon/contributors/" title="All Contributors" class="btn btn-primary">All Contributors</a>
</div>

Related posts

1 comment

  1. If you want to exclude Subscribers, the better solution would be to use the ‘exclude’ key when you query for users as the first 15 users might be subscribers and you’ll end up with no user when you apply your filter. Try the following code to fetch filtered users.

    $subscribers = get_users( array('role' => 'subscriber' ,'fields' => 'ID') );
    if ( !empty($subscribers ))
        $subscribers_str = implode(',', $subscribers);
    else 
        $subscribers_str = '';
    
    $args = array(
        'number' => 15,
        'orderby' => 'display_name',
        'order' => 'ASC',
        'exclude' => $subscribers_str
        );
    
    $allUsers = get_users($args);
    

Comments are closed.