I'm loading a set of entries related to 'category' and ordering them by the value of a Dropdown field in the entries. This work well using this code:

{% set entries = craft.entries.relatedTo(category).order('myDropdownField') %}

Now I need to change the Dropdown field to a Category field, but I am not able to sort my entries by this new category field by swapping the dropdown with the category field like this:

{% set entries = craft.entries.relatedTo(category).order('myNewCatField') %}

It gives me this error:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'departmentCat' in 'order clause'.

I also tried to use the Craft group filter, but I could not make it work in my setting.

Any suggestions are appreciated!

2 Answers 2


Here's a solution using the group filter.

{% set entriesGrouped = craft.entries.relatedTo(category).order('title')|group('myNewCategoriesField.first().title') %}

{% for group in entriesGrouped %}
    {% for entry in group %}

        {# ... #}

    {% endfor %}
{% endfor %}
  • Good solution... sounds a lot less processor intensive than doing a bunch of additional queries. Commented Feb 17, 2015 at 2:21
  • This does 99% of what I'm after, to nail the last percent I need to make the groups order manually from the order of the 'myNewCategoriesField'. If I remove 'order' and 'title' it changes the sorting {% set entriesGrouped = craft.entries.relatedTo(category) | group('myNewCategoriesField.first()') %} But instead of beeing sorted by the 'drag-order' of the categories they are sorted by the creation order of the categories. Categories seem to get their id by the order they are created not the order they are manually sorted by. Any suggestion for nailing this last detail?
    – Alf Vestre
    Commented Feb 17, 2015 at 8:39
  • @AlfVestre this is not possible with this approach, I'm afraid.
    – carlcs
    Commented Feb 17, 2015 at 9:57
  • I it possible to implement this solution but with category fields not required. So it would fall back on normal entry titles when no category is added to the category field
    – KSPR
    Commented Mar 27, 2018 at 11:03
  • @KSPR probably best to open a new question for it.
    – carlcs
    Commented Mar 28, 2018 at 10:23

As far as I'm aware, you can only order by columns in the content table. Relationships (like categories) are stored in a separate table, and are returned as arrays (or array-like objects in the case of ElementCriteriaModels) and thus not really possible to sort on.

What you may need to do is use both 'category' and 'departmentCat' in the ElementCriteriaModel.

{% set departmentCategories = craft.categories.group('departmentCat') %}
{% for departmentCategory in departmentCategories %}
    {% set entries = craft.entries.relatedTo(
        { targetElement: category }, 
        { targetElement: departmentCategory }
    ).orderBy('title) %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}

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.