0

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 ?

1

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();
}
1
  • Thats perfect. Thank you for the thorough answer
    – Spotd
    Dec 5 '17 at 20:06

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.