Sorting loops

Can someone show me an example of how the twig sort filter works? I can’t find good examples anywhere. I have this:

{% set page_newsletter = page.find('/parents/newsletter') %}
{% for p in page_newsletter.children %}
{% endfor %}

I only want to get the first child page under /parents/newsletter ordered by date and sorted either asc or desc (so the oldest newsletter, or the latest).


  1. Obligatory reference to the documentation

  2. There are many themes that use the sort function. I’ve only created one, but here is one instance in that theme.

Hi Perlkonig, sorry - I did read that doc page but it doesn’t tell me anything at all about how to use the actual function… and I still can’t see, even from your example. Do you know how I could use the sort filter to sort a loop by date? I don’t get it…

It completely depends on how your data and templates are structured. The typical way is through page collections, which is how all the blog templates work.

Hi Perlkonig, yeah I ended up working out how to make a page collection work for this. Thanks