TransWikia.com

Is it possible exclude an entry type from result of craft.entries?

Craft CMS Asked by hamishtaplin on November 4, 2020

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

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

Correct answer by Paul on November 4, 2020

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

Answered by Mark Notton on November 4, 2020

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

Answered by darylknight on November 4, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP