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.

7 Responses

Mar 04 2009 23:21

Agree with you. When you have team behind your Wordpress blog, all on different positions, it is very useful to have them marked and separated in meaningful order and syntax.

Mar 05 2009 06:23

Very useful. Thanks wprecipes and John Kolbert

Mar 05 2009 07:20

Nice hack. Luckily my blog is a one man show. I will not need it now but who know of the future. Saving these codes for future reference. Thanks

Mar 05 2009 12:24

Yeah, having people working together on a blog is a great idea. Especialy if everyone’s their role and does their own duties.

Mar 05 2009 19:30

Nice. I have a blog and every now and then I have my co-owner write something, but it’s not a big deal. Useful tip though.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required