4

I'd like to get a certain set of entries in a specific order, but the element query comes back ordered by the default 'postDate desc'

{% set ids = ['4213','3200','4204','4212']
 %}

{% set orderedEntries = craft.entries().id(ids).order('id').all() %}

{{ dump(orderedEntries.ids) }} <- not the same order as the array!

It feels like there should be a relatively simple way of doing this in craft/twig, am I missing something? Or should I just resort to SuperSort?

1 Answers1

6

You can do this by adding .fixedOrder(true) to your query:

{% set ids = ['4213','3200','4204','4212'].fixedOrder(true) %}

https://docs.craftcms.com/v3/dev/element-queries/tag-queries.html#fixedorder

Jamie Wade
  • 3,644
  • 2
  • 16
  • 32
  • Thanks, how did i miss that?! – Toby Stokes Apr 15 '19 at 10:33
  • Came across this trying to pass a list of entry ids from an entry field into a Sprig component with pagination (Craft v4.4.x). {% set entryQuery = craft.entries().id(ids).fixedOrder(true).limit(limit) %} – Jeff Irwin May 02 '23 at 14:58