1

Wondering if I can get user groups directly after retrieving the user via craft()->users->getUserById($id), or if i need to use craft()->userGroups->getGroupsByUserId($id) also?

As of right now, I'm looping through many users and want to check if they are in a specific group. A rough idea of what I'm looking at now:

// get user (necessary for rest of plugin)
$user = craft()->users->getUserById( $uid );

// get user groups
$user_groups = craft()->userGroups->getGroupsByUserId( $uid );

// check if user is in group
if( in_array("Group_Name", $user_groups) )
{
   // do something
}

This works, but when looping through a ton of users, I imagine it would be silly not to first check that I can already get this information from the $user variable I created. I can't seem to find the answer via documentation. Hoping to get a confirmation from someone!

Thanks

4

Your code to get a user returns a UserModel (as can be seen from the Full Class Reference docs):

$user = craft()->users->getUserById($id);

You can then get the user groups the $user is a member of usinggetGroups()(from the UserModel class):

$user_groups = $user->getGroups();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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