I would like to fetch all entries apart from those of a specific type: 'chapter'. How do I achieve this with a standard call to 'craft.entries'?

{% set results = craft.entries({
  'search': query,
  'order': 'score'
}) %}

Going through Craft's docs, I found no easy or official way to do this. Fastest possible way, in my opinion:

{# get all section IDs #}
{% set sections = craft.sections.getAllSections() %}

{# get all entry types for sections #}
{% set entryTypes = [] %}
{% for section in sections %}
    {# exclude the entry type you don't want, add the others to 'entryTypes' variable
       you could, of course, replace handle by ID or whatever you like #}
    {% for entryType in section.getEntryTypes() if entryType.handle != 'handleToExclude' %}
        {% set entryTypes = entryTypes | merge([entryType]) %}
    {% endfor %}
{% endfor %}

{% set entries = craft.entries.section('inhoud').type(entryTypes) %}
| improve this answer | |
  • This is what I tried but it doesn't work—I get 0 results. – hamishtaplin Nov 19 '14 at 10:04
  • Could you show me your code? – Paul Nov 19 '14 at 11:45
  • Exactly as you have it there, I get no results. – hamishtaplin Nov 20 '14 at 9:06
  • Did you replace entryTypeHandle with your entry type's handle? – Paul Nov 20 '14 at 11:47
  • 2
    Ok. You got me there. My solution doesn't work, and after diving into Crafts code I know why: the type criterium does not accept 'not'. It only accepts a string containing the entry type's handle, an Int with the entry type's id or an entryTypeModel. Or an array containing one or more of those. Let me think about the best way to work around this for a moment. – Paul Nov 20 '14 at 12:29

You can target the entry by ID and tell it 'not' to include it. The example below will exclude the current entry page. You can replace "#{entry.id}" with the ID of the entry you want to exclude.

{% set results = craft.entries({
  'id': "not #{entry.id}",
  'search': query,
  'order': 'score'
}) %}
| improve this answer | |
  • 1
    Sorry if unclear but I wish to exclude all entries of an entry type, not a single entry. – hamishtaplin Nov 18 '14 at 16:15

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.