DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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!



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

  2. 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

Leave a Comment

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