5

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'
}) %}

3 Answers 3

7

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) %}
11
  • This is what I tried but it doesn't work—I get 0 results. Commented Nov 19, 2014 at 10:04
  • Could you show me your code?
    – Paul
    Commented Nov 19, 2014 at 11:45
  • Exactly as you have it there, I get no results. Commented Nov 20, 2014 at 9:06
  • Did you replace entryTypeHandle with your entry type's handle?
    – Paul
    Commented Nov 20, 2014 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
    Commented Nov 20, 2014 at 12:29
2

If anyone lands on this page same as I did, the far far simpler way to do this is just to include the .type() method and combine it with the 'not' word:

{% set results = craft.entries({
  'search': query,
  'type': 'not chapter',
  'order': 'score'
}) %}
1
  • This is the correct modern answer to the OP
    – Adrian
    Commented Oct 21, 2022 at 14:04
1

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'
}) %}
1
  • 1
    Sorry if unclear but I wish to exclude all entries of an entry type, not a single entry. Commented Nov 18, 2014 at 16:15

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.