S
S
Sergey Miller2021-01-10 16:33:22
MODX
Sergey Miller, 2021-01-10 16:33:22

How to display all users of a certain group in Modx Revo?

Is it possible to remove all users from a specific group?

I write everything in a snippet, but I can also write in an external file using api Modx

//Инициализируем запрос
$query = $modx->newQuery('modUser');
// выполняем запрос
$users = $modx->getCollection('modUser',$query);

// цикл по пользователям
foreach ($users as $user) {

...
}

So it will turn out to display all users of the site, but how from a specific group? For example, from the boys group

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Lunegov, 2021-01-11
@blackseabreathe

If it is known that users are only in this group or this is their main group, then everything is easy.

$groupName = 'boys';
//Получаем ID группы
$groupId = $modx->getObject('modUserGroup', array('name' => $groupName))->get('id');

//Получаем коллекцию объектов пользователей с выборкой по группе
$users = $modx->getCollection('modUser', array('primary_group' => $groupId));

foreach ($users as $user) {
    
}

But if users can belong to several groups and the list is needed not by the main group, then it’s a little more complicated ...
$groupName = 'boys';
//Получаем ID группы
$groupId = $modx->getObject('modUserGroup', array('name' => $groupName))->get('id');

//Получаем коллекцию объектов групп пользователей с выборкой по группе
$userGroupMembers = $modx->getCollection('modUserGroupMember', array('user_group' => $groupId));

//прогоняем коллекцию через цикл
foreach ($userGroupMembers as $userGroupMember) {

//Получаем объект пользователя через объект группы
    $user = $userGroupMember->getOne('User');

//Делаем с объектом пользователя, что пожелаем...
}

D
Danny Arty, 2021-01-10
@DanArst

You can use the pdoUsers snippet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question