Skip to content

Instantly share code, notes, and snippets.

@gh640
Created February 14, 2015 04:42
Show Gist options
  • Save gh640/f6c5ce26a629259101a7 to your computer and use it in GitHub Desktop.
Save gh640/f6c5ce26a629259101a7 to your computer and use it in GitHub Desktop.
Drupal 7 function to get all users with specific role(s)
/**
* get all users with specific role(s)
*
* @param int|int[] $role_id
* role id for role
*
* @return object[]
* user objects with given role(s)
*/
function user_load_multiple_by_role($role_id) {
if (is_int($role_id)) {
$role_id = array($role_id);
}
$user_ids = db_select('users_roles', 'ur')
->fields('ur', array('uid'))
->condition('rid', $role_id, 'IN')
->execute()
->fetchCol();
return (count($user_ids) > 0) ? entity_load('user', $user_ids): array();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment