TransWikia.com

Filter users: in group AND not in other group

Craft CMS Asked by user10961 on October 1, 2021

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

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

Correct answer by Oli on October 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP