I am using the group filter to group like entries together. I need to be able to control which group is output first. How can I do this?


Rather than changing the order of the actual array returned by the group filter, you can just assemble a new array that has the keys in the order you want:

{# Get the grouped entries #}
{% groups = entries|group('foo') %}

{# Create an array of the group keys in the desired order #}
{% set keys = ['foo']|merge(groups|keys|without('foo')) %}

{# Now loop through the keys, rather than the groups directly #}
{% for key in keys %}

    {# Get the group with this key #}
    {% set group = groups[key] ?? [] %}

    {# Loop through the entries #}
    {% for entry in group %}
    {% endfor %}

{% endfor %}

(Note that the ?? operator requires Craft 2.6.)

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.