Answers.Body: <p>Your code to get a user returns a <code>UserModel</code> (as can be seen from the <a href="https://craftcms.com/classreference/services/UsersService#getUserById-detail" rel="noreferrer">Full Class Reference docs</a>):</p>
  
  <pre><code>$user = craft()-&gt;users-&gt;getUserById($id);
  </code></pre>
  
  <p>You can then get the user groups the <code>$user</code> is a member of using<code>getGroups()</code>(<a href="https://craftcms.com/classreference/models/UserModel#getGroups-detail" rel="noreferrer">from the <code>UserModel</code> class)</a>:</p>
  
  <pre><code>$user_groups = $user-&gt;getGroups();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-08T09:35:46.280Z
Answers.LastActivityDate: 2015-12-08T09:35:46.280Z
Answers.UserDisplayName: Steve Rowling
Body: <p>Wondering if I can get user <code>groups</code> directly after retrieving the user via <code>craft()-&gt;users-&gt;getUserById($id)</code>, or if i need to use <code>craft()-&gt;userGroups-&gt;getGroupsByUserId($id)</code> also?</p>
  
  <p>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:</p>
  
  <pre><code>// get user (necessary for rest of plugin)
  $user = craft()-&gt;users-&gt;getUserById( $uid );
  
  // get user groups
  $user_groups = craft()-&gt;userGroups-&gt;getGroupsByUserId( $uid );
  
  // check if user is in group
  if( in_array("Group_Name", $user_groups) )
  {
     // do something
  }
  </code></pre>
  
  <p>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 <code>$user</code> variable I created. I can't seem to find the answer via documentation. Hoping to get a confirmation from someone!</p>
  
  <p>Thanks</p>
  
Tags: <plugin-development><users><php><user-groups>
Title: Get Groups from User in plugin
ThreadText: # Get Groups from User in plugin
  
  (Asked by: username_0 on 2015-12-08)
  
  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
  
  **Tags:** plugin-development, users, php, user-groups
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-12-08. Score: 5 | Accepted answer
  
  Your code to get a user returns a `UserModel` (as can be seen from the [Full Class Reference docs](https://craftcms.com/classreference/services/UsersService#getUserById-detail)):
  
  ```
  $user = craft()->users->getUserById($id);
  
  ```
  
  You can then get the user groups the `$user` is a member of using`getGroups()`([from the `UserModel` class)](https://craftcms.com/classreference/models/UserModel#getGroups-detail):
  
  ```
  $user_groups = $user->getGroups();
  
  ```
  
  
  ---
  
  