How to get a list of all user groups in Modx Revolution

1.1k views Asked by At

Trying to get a list of group names from modx revolution, digging through the API and docs, but not having much luck finding a function that does this.

How can I get a list of groups [names & ids] in a snippet from a modx revolution instance>?

3

There are 3 answers

0
Vasis On BEST ANSWER
<?php
$where = array();
$userGroups = $modx->getCollection('modUserGroup', $where);
foreach ($userGroups as $userGroup) {
    print $userGroup->get('name');
}
0
Lemon On

You can do it without the loop.

<?php
$q = $modx->newQuery('modUserGroup'); 
$q->select(['name']);
$q->prepare();
$q->stmt->execute();
$userGroups = $q->stmt->fetchAll(PDO::FETCH_COLUMN);
print_r($userGroups);
0
Gleb On
<?php
//Group IDs array
print_r($modx->user->getUserGroups());
//Group Names array
print_r($modx->user->getUserGroupNames());