<p>If I understand you correctly, you want to exclude any entry that has any user from a certain user group selected in a certain Users field.</p>

<p>Here's an approach that I believe would work:</p>

<pre><code>{# Get the users that should be excluded #}
{% set excludedUsers = craft.users.group('groupToExclude').limit(null) %}

{# Get the entries that should be excluded #}
{% set excludedEntries = craft.entries.section('logsQA').relatedTo({
    targetElement: excludedUsers.ids(),
    field: 'logGeneral_sonographerName'
}).limit(null) %}

{# Get the entries that should be displayed #}
{% set entries = craft.entries.section('logsQA').id('and, not '~excludedEntries.ids()|join(', not ')) %}
</code></pre>

<p>What the above does is to first pull the users that should be excluded (you could use the <code>groupId</code> parameter instead of <code>group</code> if you want to), before using those users to pull the entries that should be excluded.</p>

<p>Finally, the entries that should be displayed are pulled – the key here is the use of the <code>id</code> parameter to specifically exclude a list of entry IDs.</p>

CC BY-SA 3.0
2016-11-13T17:27:31.670Z
This worked perfectly!  Thanks @Mats.  One thing I missed at first was to replace the "groupToExclude" in the first set variable with the group name.  Just pointing it out in case anyone else misses that too.
Lori
CC BY-SA 3.0
2016-11-13T17:33:00.840Z
Awesome! Right, "groupToExclude" is just a placeholder, and should be replaced with the real handle for the group you want to exclude.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-11-07T23:16:48.223Z
2016-11-07T23:16:48.223Z
Mats Mikkel Rummelhoff
<p>I would like to exclude entries from appearing in the template results that contain Users from specific User group.</p>

<p>My code below works if <strong>all</strong> the User(s) in the RelatedTo field are part of the excluded User group, but it doesn't work if the field contains Users from both the excluded group and a non-excluded group. </p>

<pre><code>{% set excludeFellows = craft.users.groupId('not 11') %}

{% set entries = craft.entries.section('logsQA').logGeneral_statusWorkflow('submitted').relatedTo(
    { targetElement: excludeFellows, field: 'logGeneral_sonographerName' }
) %}
</code></pre>

<p>(Also if it's better to check for specific User IDs rather than a User Group for my excluded list that's fine too. I only have 3 Users to exclude.)</p>

<templating><users><relations><relatedto>
Filter results by users contained in a Users Relations RelatedTo field
# Filter results by users contained in a Users Relations RelatedTo field

(Asked by: username_0 on 2016-11-07)

I would like to exclude entries from appearing in the template results that contain Users from specific User group.
My code below works if all the User(s) in the RelatedTo field are part of the excluded User group, but it doesn't work if the field contains Users from both the excluded group and a non-excluded group. 
{% set excludeFellows = craft.users.groupId('not 11') %}

{% set entries = craft.entries.section('logsQA').logGeneral_statusWorkflow('submitted').relatedTo(
    { targetElement: excludeFellows, field: 'logGeneral_sonographerName' }
) %}

(Also if it's better to check for specific User IDs rather than a User Group for my excluded list that's fine too. I only have 3 Users to exclude.)

**Tags:** templating, users, relations, relatedto

---

## 1 answers

### Answer by username_1 on 2016-11-07. Score: 3 | Accepted answer

If I understand you correctly, you want to exclude any entry that has any user from a certain user group selected in a certain Users field.
Here's an approach that I believe would work:
{# Get the users that should be excluded #}
{% set excludedUsers = craft.users.group('groupToExclude').limit(null) %}

{# Get the entries that should be excluded #}
{% set excludedEntries = craft.entries.section('logsQA').relatedTo({
    targetElement: excludedUsers.ids(),
    field: 'logGeneral_sonographerName'
}).limit(null) %}

{# Get the entries that should be displayed #}
{% set entries = craft.entries.section('logsQA').id('and, not '~excludedEntries.ids()|join(', not ')) %}

What the above does is to first pull the users that should be excluded (you could use the groupId parameter instead of group if you want to), before using those users to pull the entries that should be excluded.
Finally, the entries that should be displayed are pulled – the key here is the use of the id parameter to specifically exclude a list of entry IDs.

> **Comments:**
> **username_0**: This worked perfectly!  Thanks @Mats.  One thing I missed at first was to replace the "groupToExclude" in the first set variable with the group name.  Just pointing it out in case anyone else misses that too.  
> **username_1**: Awesome! Right, "groupToExclude" is just a placeholder, and should be replaced with the real handle for the group you want to exclude.  

---

