I'm getting the following SQL error when trying to simply order related entries in a Matrix block. Anyway around this?

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous. The SQL statement executed was: SELECT elements.id, elements.type, elements.enabled, elements.archived, elements.dateCreated, elements.dateUpdated, ...

This is what's throwing the error:

{% set package = craft.entries({
relatedTo: block,
order: 'sortOrder'}) %}
  • Are you working in Craft 2 or Craft 3?
    – a-am
    Commented Aug 28, 2018 at 16:25
  • I'm using Craft 2. Updated the question. Commented Aug 28, 2018 at 16:40
  • 1
    I don't believe you need the order: 'sortOrder' it does that by default when you access related entries. Here is another post that may be of help. craftcms.stackexchange.com/questions/4654/…
    – a-am
    Commented Aug 28, 2018 at 17:39
  • Thanks @aran but no, it's not obeying the sortOrder. I only seem to come across this issue on a related entry within a Matrix field block. Commented Aug 29, 2018 at 8:51

1 Answer 1


So this was me being a bit of a numpty here. I was calling the field the wrong way. Doing the following obeys the sort order from the field:

{% for entry in block.hostingPackage %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.