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'
}) %}
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityI 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) %}
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'
}) %}
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'
}) %}