Connect with us

How to Get All Users With a Specific Role


How to Get All Users With a Specific Role

This WordPress tutorial shows you how to get all users with a specific role. This can be used for mass emailing subscribers, mass changing user roles, or whatever your developing heart desires!

Sometimes as a WordPress plugin developer you want to get all of the users with a specific role. For example, maybe you want to create a plugin that emails all the “Editors” of your blog. To easily get all users of a role, use the function below.

function getUsersWithRole($role) {
//gets all users with specified role
      $wp_user_search new WP_User_Search($usersearch$userspage$role);
      return $wp_user_search->get_results();

The getUsersWithRole() function returns an array containing the user IDs of all users with the role you specified. To use this, just set the function to a variable and loop through the array.

$editors = getUsersWithRole('editor');
foreach($editors as $editor){
     //$editor now holds the user ID of an editor

Update: It’s important to note that this function only works on admin pages of WordPress (such as plugin or theme options pages). It is not available for theme template usage.

Related:  Simple Procedure to Fix ” Are You Sure You Want to Do This ” Error
Continue Reading
You may also like...

We are a WordPress plugin developer company that focuses on useful WordPress plugin creation and empowering people to earn passive incomes from their blogs. We build unique and groundbreaking plugins that will revolutionize your blog!

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in solution

    Sign up for our newsletter and get a free affiliate e-book!

    About Me:

    Szabi Kisded

    Hi, my name is Szabi and I'm documenting my journey earning an online (semi)passive income. Learn more

    Mega Plugin Bundle:

    CodeCanyon Portfolio:

    Online Courses:

    Recommended Theme:

    Popular Posts:

    Latest Posts:

    To Top