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.

  • Do I understand correctly that you wish to display separate lists of users for each user role? – engelen Mar 27 '16 at 13:15
  • @engelen I want to list only users having subscriber role – Kevin S Mar 27 '16 at 13:18
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'
) );
  • 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 Mar 27 '16 at 13:32
  • Yes — even though the functionality stay the same, as none of the users has the administrator role anyway. – engelen Mar 27 '16 at 13:34

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.