Wordpress - How can I get a list of users by their role?
There may be some different way to do that, but most proper way to do that is following.
<?php
$args = array(
'role' => 'Your desired role goes here.',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
echo '<ul>';
foreach ( $users as $user ) {
echo '<li>' . esc_html( $user->display_name ) . '[' . esc_html( $user->user_email ) . ']</li>';
}
echo '</ul>';
?>
Here the simple approach to grouping roles.
$wp_roles = wp_roles();
$result = count_users();
foreach ( $result['avail_roles'] as $role => $count )
{
if ( 0 == $count )
continue; //pass role none
$args = array(
'role' => $role
);
$users = get_users( $args );
$user = array();
for ( $i = 0; $i < $count ; $i++ )
$user[] = esc_html( $users[ $i ]->display_name ); //show display name
//output
echo wp_sprintf( '<h2>%1$s</h2><ul><li>%2$s</li></ul>',
esc_html( $wp_roles->role_names[ $role ] ),
implode( '</li><li>', $user )
);
}
When you find users with Ultimate Member Plugin Roles, You have to add "um_" to your role value. For example, you created the role name "Client" in Ultimate Membership Plugin, then $args would be
$args = array(
'role' => 'um_client',
'orderby' => 'user_nicename',
'order' => 'ASC'
);