0

I have a bunch of entries in a structure and I want to have all the "featured" entries returned first. Then everything else returned in the order set for that structure in the admin.

The answer to a similar question (Order Entries Query by a Featured Light Switch) works well for channels, but not for structures…

craft.entries().section('mySection').orderBy('featured DESC').all()

☝️ This seems to be defaulting to postDate as a "second" parameter, as opposed to "the order defined by the section" like the documentation seems to indicate it should do for a structure.

Is this a bug, or is there a way to explicitly order by the structure's ordering in a second parameter?

Thanks!

1 Answer 1

1

Since you've overriden the orderBy parameter, you'll need to specify the second thing you want to order by. Structures store order and hierarchy using a few extra properties. You can use the lft property to sort the structures entries, meaning that the following code should solve your question.

{% set entries = craft.entries
    .section('structure')
    .orderBy({ 'featured': SORT_DESC, 'lft': SORT_ASC })
    .all() 
%}
2
  • Thank you! Where do I find this lft property? I was looking all over the docs but couldn't find it… Thanks again!
    – Yann
    Jul 26 at 21:43
  • You'll find it in the structureelements database table, it is likely not documented because it is used internally, along with a few other properties, to store the element's position within the structure.
    – Ben Croker
    Jul 27 at 7:28

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.