<p>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.</p>

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

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

CC BY-SA 3.0
2015-02-16T22:41:49.177Z
2015-02-16T22:41:49.177Z
Douglas McDonald
<p>Here's a solution using the group filter.</p>

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

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

        {# ... #}

    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-02-17T02:21:28.773Z
Good solution... sounds a lot less processor intensive than doing a bunch of additional queries.
Douglas McDonald
CC BY-SA 3.0
2015-02-17T08:39:39.037Z
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
CC BY-SA 3.0
2015-02-17T09:57:46.323Z
@AlfVestre this is not possible with this approach, I'm afraid.
carlcs
CC BY-SA 3.0
2018-03-27T11:03:23.380Z
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
CC BY-SA 3.0
2018-03-28T10:23:32.393Z
@KSPR probably best to open a new question for it.
carlcs
CC BY-SA 3.0
2015-02-16T23:23:27.987Z
2015-02-16T23:23:27.987Z
carlcs
<p>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:</p>

<pre><code>{% set entries = craft.entries.relatedTo(category).order('myDropdownField') %}
</code></pre>

<p>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:</p>

<pre><code>{% set entries = craft.entries.relatedTo(category).order('myNewCatField') %}
</code></pre>

<p>It gives me this error:</p>

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

<p>I also tried to use the Craft <a href="http://buildwithcraft.com/docs/templating/filters#group" rel="noreferrer">group</a> filter, but I could not make it work in my setting.</p>

<p>Any suggestions are appreciated!</p>

<categories><ordering>
Order entries by category field
# Order entries by category field

(Asked by: username_0 on 2015-02-16)

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!

**Tags:** categories, ordering


---

## 2 answers

### Answer by username_2 on 2015-02-16. Score: 9 | Accepted answer

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 %}

> **Comments:**
> **username_1**: Good solution... sounds a lot less processor intensive than doing a bunch of additional queries.  
> **username_0**: 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?  
> **username_2**: @AlfVestre this is not possible with this approach, I'm afraid.  
> **username_3**: 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  
> **username_2**: @username_3 probably best to open a new question for it.  

---

### Answer by username_1 on 2015-02-16. Score: 2

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(
        'and', 
        { targetElement: category }, 
        { targetElement: departmentCategory }
    ).orderBy('title) %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}


---

