On my search results page I want to add a section filter depending on the different sections that are included in all of the returned entries.

I am getting all the sections like this:

{% set entries = craft.entries.section(['inspirationArticles', 'pages', 'pressArticles', 'products']).search(searchQuery).order('score') %}
{% set entrySections = entries.getAllSections() %}

And then loop through them:

{% for entrySection in entrySections %}
{% endfor %}

But when trying to access the sections’s names or handles via {{ entrySection.name }} or {{ entrySection.handle }}, I get a CException error:

Craft\EntryModel and its behaviors do not have a method or closure named "handle".

So it looks like it isn't actually accessing the SectionModel. Am I missing a step?


Entries to not have a getAllSections method. You can use the group filter to group your entries by their sections.

{% set entries = craft.entries.section(['inspirationArticles', 'pages', 'pressArticles', 'products']).search(searchQuery).order('score') %}
{% set grouped = entries|group('section') %}

{% for section, entriesInGroup in grouped %}
    <h2>Section: {{ section }}</h2>

        {% for entry in entriesInGroup %}
        {% endfor %}
{% endfor %}

Here's some documentation to help!

  • Thanks Aaron, that works like a charm! Though I do kinda wish I could get a way a one less for loop to get to the sections’ details, but that's just me being picky ;) – Katrin Jun 12 '15 at 17:36

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.