<p>The probably more elegant way to handle this is to not use the <code>without</code> filter at all and simply
list the categories you want to exclude prefixed with <code>not</code>.</p>

<pre><code>{% set categories = craft.categories({
    group: 'location',
    slug: [
        'and',
        'not berlin',
        'not amsterdam',
    ],
    limit: null,
}) %}

{% set entries = craft.entries.section('news').relatedTo(categories) %}

{% paginate entries as entriesOnPage %}
    ...
{% endpaginate %}
</code></pre>

CC BY-SA 3.0
2017-01-05T10:10:17.520Z
Hm, this looks like it could be a really nice solution, but would there be a problem with this whereby any entries which didn't have _any_ categories in the `location` group also wouldn't show? (i.e. any entry would have to have at least one category in the `location` group for it to show).
Mike Vaux
CC BY-SA 3.0
2017-01-05T11:00:09.223Z
Yes, this only returns entries that are related to at least one of the categories. Useful if you have set the categories field to be required. You can add more groups to the `group` param or don't use it at all.
carlcs
CC BY-SA 3.0
2017-01-05T16:13:40.593Z
Ok, thanks - just wanted to make sure I wasn't missing anything! :) Good shout re. using this if the categories field is required.
Mike Vaux
CC BY-SA 3.0
2015-04-22T08:37:01.820Z
2015-04-22T08:37:01.820Z
carlcs
<p>The <code>without</code> filter converts your ElementCriteriaModel into an array of entry models (the same as <code>find()</code> does). But that array won't work with <code>paginate</code>, as explained in the docs:</p>

<blockquote>
  <p>This parameter needs to be an actual ElementCriteriaModel object; not an array of elements. So don’t call find() on the object.</p>
</blockquote>

<p>So you need to query for the entries you're after directly with an ElementCriteriaModel. The workaround to get there would be to pass an array of entry IDs into the <code>id()</code> parameter.</p>

<pre><code>{% set categoriesToExclude = craft.categories({slug: 'berlin'}) %}
{% set entriesToExcludeIds = craft.entries.section('news').relatedTo(categoriesToExclude).limit(null).ids() %}
{% set entryIds = craft.entries.section('news').limit(null).ids()|without(entriesToExcludeIds) %}

{% set entries = craft.entries.id(entryIds) %}

{% paginate entries as entriesOnPage %}
    ...
{% endpaginate %}
</code></pre>

<p>It's not recommended to use the <code>without</code> filter with objects anyways (→ see <a href="https://craftcms.stackexchange.com/a/7178/125">Brad's answer</a> to a related question)!</p>

CC BY-SA 3.0
2015-04-21T12:52:16.037Z
2015-04-22T08:36:47.007Z
2017-04-13T12:51:00.200Z
carlcs
<p>I'm trying to paginate a channel of entries but I also need to exclude any entries that relate to a specific category.</p>

<p>This is what I have so far...</p>

<pre><code>{% set insightCategory = craft.categories({"slug": "insights"}).first() %}
{% set insightsEntryIds = craft.entries.section('blogChannel').relatedTo({targetElement: insightCategory}).limit(null).ids() %}

{% paginate craft.entries.section('blogChannel')|without(insightsEntryIds) as entries %}
    {% for blogEntry in entries %}
        &lt;p&gt;{{ blogEntry.title }} ({{ blogEntry.id }})&lt;/p&gt;
    {% endfor %}
{% endpaginate %}
</code></pre>

<p>Unfortunately the pagination doesn't seem to work with the |without filter. How can I achieve this?</p>

<p>Thanks</p>

<filter><pagination><channels>
Exclude category from channel entries (with pagination)
# Exclude category from channel entries (with pagination)

(Asked by: username_0 on 2015-04-21)

I'm trying to paginate a channel of entries but I also need to exclude any entries that relate to a specific category.
This is what I have so far...
{% set insightCategory = craft.categories({"slug": "insights"}).first() %}
{% set insightsEntryIds = craft.entries.section('blogChannel').relatedTo({targetElement: insightCategory}).limit(null).ids() %}

{% paginate craft.entries.section('blogChannel')|without(insightsEntryIds) as entries %}
    {% for blogEntry in entries %}
        {{ blogEntry.title }} ({{ blogEntry.id }})
    {% endfor %}
{% endpaginate %}

Unfortunately the pagination doesn't seem to work with the |without filter. How can I achieve this?
Thanks

**Tags:** filter, pagination, channels


---

## 2 answers

### Answer by username_1 on 2015-04-21. Score: 6 | Accepted answer

The without filter converts your ElementCriteriaModel into an array of entry models (the same as find() does). But that array won't work with paginate, as explained in the docs:

This parameter needs to be an actual ElementCriteriaModel object; not an array of elements. So don’t call find() on the object.

So you need to query for the entries you're after directly with an ElementCriteriaModel. The workaround to get there would be to pass an array of entry IDs into the id() parameter.
{% set categoriesToExclude = craft.categories({slug: 'berlin'}) %}
{% set entriesToExcludeIds = craft.entries.section('news').relatedTo(categoriesToExclude).limit(null).ids() %}
{% set entryIds = craft.entries.section('news').limit(null).ids()|without(entriesToExcludeIds) %}

{% set entries = craft.entries.id(entryIds) %}

{% paginate entries as entriesOnPage %}
    ...
{% endpaginate %}

It's not recommended to use the without filter with objects anyways (→ see Brad's answer to a related question)!


---

### Answer by username_1 on 2015-04-22. Score: 5

The probably more elegant way to handle this is to not use the without filter at all and simply
list the categories you want to exclude prefixed with not.
{% set categories = craft.categories({
    group: 'location',
    slug: [
        'and',
        'not berlin',
        'not amsterdam',
    ],
    limit: null,
}) %}

{% set entries = craft.entries.section('news').relatedTo(categories) %}

{% paginate entries as entriesOnPage %}
    ...
{% endpaginate %}

> **Comments:**
> **username_2**: Hm, this looks like it could be a really nice solution, but would there be a problem with this whereby any entries which didn't have _any_ categories in the `location` group also wouldn't show? (i.e. any entry would have to have at least one category in the `location` group for it to show).  
> **username_1**: Yes, this only returns entries that are related to at least one of the categories. Useful if you have set the categories field to be required. You can add more groups to the `group` param or don't use it at all.  
> **username_2**: Ok, thanks - just wanted to make sure I wasn't missing anything! :) Good shout re. using this if the categories field is required.  

---

