<p>You could do this in a few steps (<em>warning: this is untested code</em>):</p>

<p><strong>1)</strong> Define an array of the sections you cited, in the correct order:</p>

<pre><code>{# Define a list of the sections you want to display in the correct order #}
{% set sections = ['news', 'products', 'reports'] %}
</code></pre>

<p><strong>2)</strong> Identify the entries that do not belong in any of these sections:</p>

<pre><code>{# Create an array of entries that do not belong to any of the sections above #}
{% set otherEntries = [] %}

{# We first loop on the entries by section to identify the "other" entries #}
{% for section, entries in entriesBySection %}
    {% if section in sections %}
        {% for entry in entries %}
            {% set otherEntries = otherEntries|merge([entry])}
        {% endfor %}
    {% endif %}
{% endfor %}
</code></pre>

<p><strong>3)</strong> Display all the entries of each section in the correct order</p>

<pre><code>{# Loop on the sections in the right order #}
{% for section in sections %}
    {# Obtain all the entries for the current section #}
    {% set entries = entriesBySection[section] %}
    {% for entry in entries %}
        ...
    {% endfor %}
{% endfor %}
</code></pre>

<p><strong>4)</strong> Display the rest of the entries</p>

<pre><code>{# Finally display the "other" entries #}
{% for entry in otherEntries %}
    ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-06-09T07:20:34.130Z
2017-06-09T07:20:34.130Z
kant312
<p>I'm grouping search results by section, based on Aaron Berkowitz's answer to <a href="https://craftcms.stackexchange.com/questions/19046/group-entries-by-section">this question</a>. My code looks like this:</p>

<pre><code>{% set entries = craft.entries({search: {query: keywords} }) %}
{% set entriesBySection = entries | group('section') %}

{% for section, entries in entriesBySection %}
    &lt;h2&gt;{{ section }}&lt;/h2&gt;
    &lt;ul&gt;
        {% for entry in entries %}
            &lt;li&gt;{{ entry.title }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endfor %}
</code></pre>

<p>That works great but I need to show the sections in a specific order, and merge some sections into one. So I'd end up with, for example:</p>

<ul>
<li>News</li>
<li>Products</li>
<li>Reports</li>
<li>Other (everything else combined)</li>
</ul>

<p>Any idea how to do this?</p>

<p>Thanks loads for any help!</p>

<entries><search><group>
Group entries by section, then order and combine sections
# Group entries by section, then order and combine sections

(Asked by: username_0 on 2017-06-08)

I'm grouping search results by section, based on Aaron Berkowitz's answer to this question. My code looks like this:
{% set entries = craft.entries({search: {query: keywords} }) %}
{% set entriesBySection = entries | group('section') %}

{% for section, entries in entriesBySection %}
    {{ section }}
    
        {% for entry in entries %}
            {{ entry.title }}
        {% endfor %}
    
{% endfor %}

That works great but I need to show the sections in a specific order, and merge some sections into one. So I'd end up with, for example:

News
Products
Reports
Other (everything else combined)

Any idea how to do this?
Thanks loads for any help!

**Tags:** entries, search, group


---

## 1 answers

### Answer by username_1 on 2017-06-09. Score: 2

You could do this in a few steps (warning: this is untested code):
1) Define an array of the sections you cited, in the correct order:
{# Define a list of the sections you want to display in the correct order #}
{% set sections = ['news', 'products', 'reports'] %}

2) Identify the entries that do not belong in any of these sections:
{# Create an array of entries that do not belong to any of the sections above #}
{% set otherEntries = [] %}

{# We first loop on the entries by section to identify the "other" entries #}
{% for section, entries in entriesBySection %}
    {% if section in sections %}
        {% for entry in entries %}
            {% set otherEntries = otherEntries|merge([entry])}
        {% endfor %}
    {% endif %}
{% endfor %}

3) Display all the entries of each section in the correct order
{# Loop on the sections in the right order #}
{% for section in sections %}
    {# Obtain all the entries for the current section #}
    {% set entries = entriesBySection[section] %}
    {% for entry in entries %}
        ...
    {% endfor %}
{% endfor %}

4) Display the rest of the entries
{# Finally display the "other" entries #}
{% for entry in otherEntries %}
    ...
{% endfor %}


---

