<p>Use the <code>ActiveRecord</code> from <a href="http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html" rel="nofollow noreferrer">Yii2</a></p>

<pre><code>$userGroupId = 1 // insert your usergroup Id here
$users = User::find()-&gt;groupId($userGroupId)-&gt;all();
</code></pre>

<p>Or via handle</p>

<pre><code>$userGroup = Craft::$app-&gt;userGroups-&gt;getGroupByHandle('handle');
$users = User::find()-&gt;group($userGroup)-&gt;all();
</code></pre>

<p>If you mean the number of users in a group with total you just have to do <code>count()</code> instead of <code>all()</code></p>

<p>To get the users of each group you can do</p>

<pre><code>foreach(Craft::$app-&gt;userGroups-&gt;getAllGroups() as $userGroup){
    $users = User::find()-&gt;group($userGroup)-&gt;all();
}
</code></pre>

CC BY-SA 3.0
2017-12-05T20:06:51.310Z
Thats perfect. Thank you for the thorough answer
Spotd
CC BY-SA 3.0
2017-12-03T20:47:41.230Z
2017-12-06T18:27:30.430Z
2017-12-06T18:27:30.430Z
Robin Schambach
<p>I have tried to find the new way get the total in a user group in php (plugin), but not having much luck... Please help
Assume it starts with Craft::$app->users ?</p>

<users><search><craft3>
Craft 3 beta - User group total
# Craft 3 beta - User group total

(Asked by: username_0 on 2017-12-03)

I have tried to find the new way get the total in a user group in php (plugin), but not having much luck... Please help
Assume it starts with Craft::$app->users ?

**Tags:** users, search, craft3


---

## 1 answers

### Answer by username_1 on 2017-12-03. Score: 1 | Accepted answer

Use the ActiveRecord from Yii2
$userGroupId = 1 // insert your usergroup Id here
$users = User::find()->groupId($userGroupId)->all();

Or via handle
$userGroup = Craft::$app->userGroups->getGroupByHandle('handle');
$users = User::find()->group($userGroup)->all();

If you mean the number of users in a group with total you just have to do count() instead of all()
To get the users of each group you can do
foreach(Craft::$app->userGroups->getAllGroups() as $userGroup){
    $users = User::find()->group($userGroup)->all();
}

> **Comments:**
> **username_0**: Thats perfect. Thank you for the thorough answer  

---

