Please could you advise on how to filter a user query, so that users from Group A are included, but not if they are also in Group B?

{% set vnp_users = craft.users
    .group(['groupA', 'not groupB'])
    .orderBy('field desc')
    .all() %}

I'm not sure you can do that in one query only but I might be wrong.

Using two queries, you could do the following:

{# Fetch users from the group you don't want to display #}
{% set usersToExclude = craft
    .ids() %}

{# Built an array of ids to exclude adding 'not' to it #}
{% set exclusionList = ['not'] | merge(usersToExclude) %}

{# Fetch your users excluding the one found above #}
{% set vnp_users = craft
    .id(exclusionList).all() %}
  • Thanks Oli, though it would be nice if the group() method could take values to match as well as values to exclude.
    – user10961
    Jul 29 '20 at 13:19

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.