1

Is it possible to perform a search and have the results grouped/prioritized by sections?

e.g. something like:

craft.entries.section(['sec3', 'sec5', 'sec1']).search(...).limit(6) as page_info, page_entries

I want sec3 entries first, followed by sec5 and sec1 entries.

Note that I want to use the built-in pagination mechanism and would not like to use hacks such as searching tons of entries and manually group the results by sections.

1

Something like this should get you started, q is the querystring in your results url:

{% set query = craft.request.getParam('q') %}
{% set entries = craft.entries.search(query).level(1).order('score') %}
{% set grouped = entries|group('section') %}

        <h1>Results for <em>&ldquo;{{ query }}&rdquo;</em></h1>

        {% if entries|length %}
        {% for section, entriesInGroup in grouped %}
        <h3>{{ section }}</h3>
            <ul>
            {% for entry in entriesInGroup %}
                <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% endfor %}
            </ul>
        {% endfor %}
{% endif %} 

Not sure how pagination fits in here but I'm sure others will chime in.

  • Thanks but I did mention that I don't want to grab all results and then group by section. Ideally the results won't be already grouped and ordered by section so that there would be not "hack" needed that would affect performance. – biko Mar 25 '17 at 11:50

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.