1

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.)

3

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.

| improve this answer | |
  • 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 Nov 13 '16 at 17:27
  • 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 Nov 13 '16 at 17:33

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.