3

I'm struggling to find a way to use craft.users to select all users that are not members of any groups. Looking at the User Element Type both the groupId and group parameters are used to perform additional queries which will return nothing unless a valid group Id or handle is provided... I think.

Is it possible to find users not in groups or will I have to do this post retrieving all users... I need to paginate the results so this is not ideal.

2 Answers 2

3

You could get the IDs of all users belonging to a group, and use these IDs prefixed with not as the id parameter in a second craft.users ElementCriteriaModel:

{% set userIds = craft.users.group('group1, group2, group3').ids() %}
{% set users = craft.users.id('and, not ' ~ userIds|join(', not ')) %}
2

I don't think it's possible, exactly for the reasons you provide: while other criteria accept 'not X', group and groupId explicitly add andWhere criteria with the Ids found with ids or handles.

You could make a plugin that returns an array containing all users without a user group, but maybe that's a bit far fetched. I do think that's the only way (for now), if you don't want to do it post retrieving ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.