How-To Wordpress

How to list users by their role WordPress?

WordPress is a powerful content management system that provides its users with a range of features and functionalities to manage their websites effectively. One of the key features of WordPress is the ability to assign roles to users, which determines what actions they can perform on the website. For example, an administrator has access to all the features of the website, while a subscriber can only view the content.

In this article, we will discuss how to list users by their roles in WordPress.

If you are the only person controlling your whole website, you have probably never had to think about WordPress users and their roles. However, if you ever want to give other people access to your WordPress website, WordPress user roles are essential for managing what actions the various users at your site are allowed to take.

WordPress user roles determine what actions each user at your site is authorized to perform. These actions are called capabilities. In this article, we are going to show how to list WordPress users by their user role.

WordPress has some pre-defined user roles :

  1. Super Admin
  2. Administrator
  3. Editor
  4. Author
  5. Subscriber
  6. Contributor

Here is the code to display the list of users, by their Specific Roles. We can also delete these default User Roles if not required for the website.

In the below array, we have given the user role as “subscriber” so it will make the list of users who is having the role “Subscriber” with their E-mail.

$args = array(
'role' => 'Subscriber',
'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>';

In conclusion, listing users by their roles in WordPress is a simple process that can be done with the help of the above code. By following the steps outlined in this article, you can easily create custom capabilities for each role and filter the users by their roles. This feature can be useful for website owners who want to manage their users effectively and ensure that the right users have access to the right content and features on their websites.

Happy Coding 🙂

About the author

Vishwas

A coder who is trying to solve some problems via "Procoders", Software Engineer By Profession, WEB Enthusiast, Hobby Blogger. I love to share Tech Ideas and like to learn and explore new things and Technologies.

Add Comment

Click here to post a comment