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.


banesto
April 8, 201011:56 amthat's nice, but how can I get:
1) not user IDs, but array of user objects
2) users by their access level (e.g. users above "author" access level)
?
Ian
June 25, 201010:17 pmYou can use it in a template if you want, you just have to include the class first:
http://jhnk.pastebin.com/8CD8VW3A
Ian
June 25, 201010:52 pmBanesto, once you have the ID you can use http://codex.wordpress.org/Function_Reference/get_userdata to get the rest of their info