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'
}) %}
hamishtaplin
  • 776
  • 8
  • 16

3 Answers3

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) %}
Paul
  • 6,338
  • 12
  • 26
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'
}) %}
darylknight
  • 3,290
  • 18
  • 42
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'
}) %}
Mark Notton
  • 2,337
  • 1
  • 15
  • 30