0

I am currently listing all the users except users with administrator role on custom page template with:

<?php
    $allUsers = get_users('orderby=post_count&order=DESC');
    $users = array();

    foreach($allUsers as $currentUser)
    {
        if(!in_array( 'administrator', $currentUser->roles ))
        {
            $users[] = $currentUser;
        }
    }
?>

In the following format:

<?php foreach($users as $user) { ?>
    <div class="profile">
        <div class="avatar">
            <?php echo get_avatar( $user->user_email, '150' ); ?>
        </div>
        <div class="profileInfo">
            <h2 class="name"><?php echo $user->display_name; ?></h2>
            <p class="descrption"><?php echo get_user_meta($user->ID, 'description', true); ?></p>
        </div>
        <div class="clear"></div>
    </div>
<?php } ?>

As I am going to add 6 more custom user group (role) so I would like to list users from specific role instead of listing all the users except administrator role.

I have already tried several tutorial but for reason I am not able to incorporate with my current listing code.

2
  • Do I understand correctly that you wish to display separate lists of users for each user role?
    – engelen
    Commented Mar 27, 2016 at 13:15
  • @engelen I want to list only users having subscriber role
    – Kevin S
    Commented Mar 27, 2016 at 13:18

1 Answer 1

0

To fetch users from a specific role, you can use the role parameter of the get_users function. Each role has a specific ID; for WordPress' native roles, these are administrator, editor, author, contributor and subscriber. In your case, you would want to fetch only subscriber users:

$allUsers = get_users( array(
    'orderby' => 'post_count',
    'order' => 'DESC',
    'role' => 'subscriber'
) );
2
  • If I use role parameter of the get_users function I will have to comment out if(!in_array( 'administrator', $currentUser->roles )) { } from old foreach loop right?
    – Kevin S
    Commented Mar 27, 2016 at 13:32
  • Yes — even though the functionality stay the same, as none of the users has the administrator role anyway.
    – engelen
    Commented Mar 27, 2016 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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