Post Pic

How to: Get all users having a specific role

When developing plugins or advanced WordPress theme, sometimes it can be useful to get all the users belonging to a specific role, for exemple, all administrators. My friend John Kolbert just wrote a great function to do that.

The first thing to do is to create the function. To do so, paste the following code in your functions.php file:

function getUsersWithRole($role) {
      $wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
      return $wp_user_search->get_results();
}

Once done, you can call the function this way:

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

Thanks to John Kolbert for this great recipe!

Related Posts

Related Posts

No related posts.

2 Responses

Feb 10 2012 16:57

You MUST SAY that it does not work on theme templates!

Dec 11 2012 21:21

Since WordPress 3.1 WP_User_Search() has been deprecated, use WP_User_Query() instead.

See the Codex: http://codex.wordpress.org/Class_Reference/WP_User_Query

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox