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() %}
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityYou 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
.entries()
.section('entries')
.leftJoin('{{%matrixblocks}} matrixblocks', '[[matrixblocks.ownerId]] = [[elements.id]]')
.leftJoin(
field.contentTable . ' myContentTable',
'[[myContentTable.elementId]] = [[matrixblocks.id]]'
)
.orderBy({'myContentTable.fieldprefix_fieldHandle': SORT_ASC})
.all()
%}
change myContentTable.fieldprefix_fieldHandle
to your exact DB column or fetch it dynamically.