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

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

        &lt;h1&gt;Results for &lt;em&gt;&amp;ldquo;{{ query }}&amp;rdquo;&lt;/em&gt;&lt;/h1&gt;

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

<p>Not sure how pagination fits in here but I'm sure others will chime in.</p>

CC BY-SA 3.0
2017-03-25T11:50:22.430Z
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
CC BY-SA 3.0
2017-03-24T11:08:58.803Z
2017-03-24T11:08:58.803Z
erwinheiser
<p>Is it possible to perform a search and have the results grouped/prioritized by sections?</p>

<p>e.g. something like:</p>

<pre><code>craft.entries.section(['sec3', 'sec5', 'sec1']).search(...).limit(6) as page_info, page_entries
</code></pre>

<p>I want <code>sec3</code> entries first, followed by <code>sec5</code> and <code>sec1</code> entries.</p>

<p>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.</p>

<entries><search><sections><order><group>
Search entries ordered by sections
# Search entries ordered by sections

(Asked by: username_0 on 2017-03-23)

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.

**Tags:** entries, search, sections, order, group

---

## 1 answers

### Answer by username_1 on 2017-03-24. Score: 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') %}

        Results for “{{ query }}”

        {% if entries|length %}
        {% for section, entriesInGroup in grouped %}
        {{ section }}
            
            {% for entry in entriesInGroup %}
                {{ entry.title }}
            {% endfor %}
            
        {% endfor %}
{% endif %} 

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

> **Comments:**
> **username_0**: 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.  

---

