I'm trying to get a list of entries of a certain section, however I'd like to exclude certain users that are defined within a field of the entry.

The users to exclude from the query are fetched like so:

{% set exclude_users = entry.winners_exclude %}

Now, what I'm trying to do is to exclude the users in theexclude_users-array from being returned with the query results, something like the following (which does obviously not work, but you might get the idea of what I'm trying:

{% for entry in craft.entries.section('bildupload').author(not in exclude_users).search('challengeId:' ~ch.id).order('likecounter desc').limit(3).with(['useruploads']) %}

Specifically I'm talking about the .author(not in exclude_users) part.

I didn't really find anything on the question if that is at all possible within the elementCriteriaModel query itself. It works with an additional if-condition within the for-loop, however since the elementCriteriaModel needs to be .limit(ed) to 3 but needs to exclude some users, excluding them within the loop after limiting the result to the first 3 results obviously results not in what I want, since it will just exclude the users from the first 3 results.

2 Answers 2


Iterating on Michael's answer: build an array of users to exclude, like this:

{% set excludeArray = null %}
{% for user in entry.winners_exclude %}
    {% if loop.first %}{% set excludeArray = ['and'] %}{% endif %} {# only set this array if exclude users exist #}
    {% set excludeArray = excludeArray |merge(['not ' ~ user.id]) %}
{% endfor %}

{# check if users should be excluded, I'm not sure what happens if you'd do .author(null) #}
{% if excludeUsers %}
    {% paginate craft.entries.section('bildupload').author(excludeUsers) %}
{% else %}
    {% paginate craft.entries.section('bildupload') %}
{% endif %}
  • Good job writing out the needed twig code :)
    – mrsmn
    Commented Sep 15, 2017 at 5:36

Have you tried building your excluded list like this ['and', 'not user1', 'not user2'] ?

Because I know syntax like that works for excluding sections in search results. Below is how I excluded certain sections from search results.

 {% paginate craft.entries.section(['and', 'not services', 'not solutions']).search(query).limit(settings.postsPerPage) as pageInfo, entries %} 
  • Thing is: I don't know which users need to be excluded beforehand. So I would need to somehow iterate over the array. Commented Sep 14, 2017 at 8:49
  • it should be possible to loop over your exclude_users array and build one that is formatted like that... I'll have a go at it soonish :)
    – mrsmn
    Commented Sep 14, 2017 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.