<p>I'm not sure you can do that in one query only but I might be wrong.</p>
<p>Using two queries, you could do the following:</p>
<pre><code>{# Fetch users from the group you don't want to display #}
{% set usersToExclude = craft
    .users()
    .group('groupToExclude')
    .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
    .users()
    .group('groupYouWant')
    .id(exclusionList).all() %}
</code></pre>

CC BY-SA 4.0
2020-07-29T13:19:34.467Z
Thanks Oli, though it would be nice if the group() method could take values to match as well as values to exclude.
user10961
CC BY-SA 4.0
2020-07-29T13:12:04.873Z
2020-07-29T13:12:04.873Z
Oli
<p>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?</p>
<pre><code>{% set vnp_users = craft.users
    .group(['groupA', 'not groupB'])
    .orderBy('field desc')
    .all() %}
</code></pre>

<users><search><filter>
Filter users: in group AND not in other group
# Filter users: in group AND not in other group

(Asked by: username_0 on 2020-07-29)

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() %}

**Tags:** users, search, filter

---

## 1 answers

### Answer by username_1 on 2020-07-29. Score: 1 | Accepted answer

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
    .users()
    .group('groupToExclude')
    .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
    .users()
    .group('groupYouWant')
    .id(exclusionList).all() %}

> **Comments:**
> **username_0**: Thanks Oli, though it would be nice if the group() method could take values to match as well as values to exclude.  

---

