When getting pages from multiple sections like so:

  {% set aboutNavigation = craft.entries
    .section(['about', 'newsIndex'])
    .all() %}

How can I set order that the sections appear? So I want the about pages to be ordered first (by their structure order (which works out the box) and then the newsIndex page to be the last page in the order.

How would this also apply if there were more than 2 sections in the array?


I've faced this a few times and solved it like this:

{% set entries = craft.entries.section([
]).all() %}

{# group the results by section handle #}
{% set entriesGrouped = entries|group('section.handle') %}

{# use this array to specify a manual order to be used when outputting the results #}
{% set manualSortOrder = [
 ] %}

{# ================================== #}

    {% for sectionHandle in manualSortOrder %}
        {% if entriesGrouped[sectionHandle] is defined %}
            {% for entry in entriesGrouped[sectionHandle] %}
                <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% endfor %}
        {% endif %}
    {% endfor %}
  • Thanks James. This is a nice approach. Oct 6 at 10:59

Since posting the original question, I have also solved this like so:

{% set aboutPages = craft.entries.section(['about']).all() %}
{% set newsPage = craft.entries.section('newsIndex').one() %}
{% set aboutNavigation = aboutPages|merge([newsPage]) %}
  • Bear in mind that this approach will increase your query count, as you're making DB roundtrips every time you call .one() or .all(). Oct 6 at 13:17

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.