I have following system roles set up for users

  1. content publisher
  2. content approver

All my users divided in to 5 organic groups.

I didn't set up any roles for organic groups.I am trying to get content approvers for particular group for approval process.

I tried using this function og_get_users_by_roles.

But the above function uses the organic group roles not system roles.

I am confused now should i setup same roles for organic groups.Please give some direction.

4 Answers 4


Try this if it works for you.call this function with user_role id and group id as parameter.

    function get_users_on_role_and_group_basis($rid, $gid) {
      $users = entity_load('user'); 
      foreach($users as $user) {
         if(is_array($user->og_user_node)) {
           if (array_key_exists($rid, $user->roles)) {
             foreach($user->og_user_node['und'] as $key=>$values) {
               if(in_array($gid,$values)) {
                 $get_user[] = $user; 
      } if(isset($get_user)) {
          return $get_user;
        }else {
        return 0;

If you would like to get a list of organic group users by their roles, there's no core og function to do that. And it doesn't seem like that's what you're after.

However, for those who have found this thread while searching for a way to get organic group users by roles, one can install the module og_extras for this function:

 * Get all the users with certain roles in a group.
 * @param $gid
 *   The group unique ID.
 * @param $roles
 *   Array with the role IDs to query.
og_extras_get_users_by_roles($gid, $rids = array())

This is clearly the wrong function to use to get system roles and I don't know of any functions that would take care of this query. You should query the og_ tables or possibly rethink your role system and use OG roles.


It is clearly you are confused. OG managed his roles and permission separately from the roles and permission of Drupal.

This is done in the configuration tab in the admin under the OG area(user permission or something like this). This is managed this way so each group bundle can mange their permission different from other groups bundles(you can also grant to each group the option to override the permission individually).

I describe two scenarios and from here you can help us direct you to how this should be done:

  1. You rely on the system roles and then the roles of OG will not take place.
  2. You rely on the permissions and roles of OG and you don't pay attention to the wide roles. This will mean that you need to create roles under the OG.

If you decide to use 2 then some you'll need to get the users from the groups(the information is done with the entity og_membership which manage the entities that belong to groups) Then you can get the users roles with the function og_get_user_roles()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.