Can I order entries by the content of a field every entry has?

{% for entry in craft.entries.section('entries').order(  *first time field of a matrix block within the entry*  ).all() %}

1 Answer 1


You need to join the Matrix table and the Matrix content table in that case to order by this specific field. LeftJoin will allow you to only join the first block. If you have several different block ids you need to leftJoin only blocks with your block type ID

{% set field = craft.app.getFields().getFieldByHandle('yourFieldHandle') %}
{% set entries = craft
    .leftJoin('{{%matrixblocks}} matrixblocks', '[[matrixblocks.ownerId]] = [[elements.id]]')
        field.contentTable . ' myContentTable',
        '[[myContentTable.elementId]] = [[matrixblocks.id]]'
    .orderBy({'myContentTable.fieldprefix_fieldHandle': SORT_ASC})

change myContentTable.fieldprefix_fieldHandle to your exact DB column or fetch it dynamically.


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.