<p>I don't think you can order by the types order, or get the types in your template (I might be wrong there though), but maybe you don't need to let this be completely dynamic? I guess the user won't be able to add new types or change the order of them after the site is made? If so, couldn't you just make an array of the types and do somthing like this:</p>

<pre><code>{% set typesInOrder = ['handle1', 'handle2', 'handle3'] %}

{% for type in typesInOrder %}
    {% for entry in craft.entries.type(type) %}
         {{ entry.title }}
    {% endfor %}
{% endfor %}
</code></pre>

<p>There is of course some limitations, especially if you need to paginate the results. And it's bad for performance to do the craft.entries queries several times, especially if you have many entrytypes. Just thought I'd share this in case it's helpful for your usecase.</p>

CC BY-SA 3.0
2015-11-24T23:12:38.793Z
André, recommend your Preparse plugin! A simple switch conditional `{% switch entry.type %}{% case 'handle1' %}1{% case 'handle2' %}2{% default %}99{% endswitch %}` and you can use `{% set entries = craft.entries.order('myPreparseHandle') %}`.
carlcs
CC BY-SA 3.0
2015-11-24T23:43:08.687Z
Yep, that works just fine. The order in this case won't change. Thanks for the idea!
cbad
CC BY-SA 3.0
2015-11-24T22:42:46.000Z
2015-11-24T22:42:46.000Z
André Elvan
<p>You could try it with a MySQL <a href="https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case" rel="nofollow"><code>CASE</code></a> expression in the <code>order</code> parameter:</p>

<pre><code>{% set orderParam = "CASE
    WHEN type = 'handle1' THEN 1
    WHEN type = 'handle2' THEN 2
    WHEN type = 'handle3' THEN 3
    ELSE 99
END" %}

{% set entries = craft.entries.section('blog').order(orderParam) %}
</code></pre>

<p>Another <em>untested</em> suggestion is to try and make this dynamic (order similar to CP):</p>

<pre><code>{% set section = craft.sections.getSectionById(2) %}

{% set orderParam = "CASE" %}

{% for entryType in section.getEntryTypes() %}
    {% set orderParam = orderParam ~ " WHEN type = '" ~ entryType ~ "' THEN " ~ loop.index %}
{% endfor %}

{% set orderParam = orderParam ~ " ELSE 99 END" %}
</code></pre>

CC BY-SA 3.0
2015-11-24T23:32:43.627Z
2015-11-24T23:45:50.707Z
2015-11-24T23:45:50.707Z
carlcs
<p>I want to sort entries by Entry Type in the order they appear when you create/edit entry types in the CP (which can be reordered). I can't seem to find an argument for order() that works. Can this be done?</p>

<entries><entry-type>
Ordering entries by Entry Type
# Ordering entries by Entry Type

(Asked by: username_0 on 2015-11-24)

I want to sort entries by Entry Type in the order they appear when you create/edit entry types in the CP (which can be reordered). I can't seem to find an argument for order() that works. Can this be done?

**Tags:** entries, entry-type

---

## 2 answers

### Answer by username_1 on 2015-11-24. Score: 3 | Accepted answer

I don't think you can order by the types order, or get the types in your template (I might be wrong there though), but maybe you don't need to let this be completely dynamic? I guess the user won't be able to add new types or change the order of them after the site is made? If so, couldn't you just make an array of the types and do somthing like this:
{% set typesInOrder = ['handle1', 'handle2', 'handle3'] %}

{% for type in typesInOrder %}
    {% for entry in craft.entries.type(type) %}
         {{ entry.title }}
    {% endfor %}
{% endfor %}

There is of course some limitations, especially if you need to paginate the results. And it's bad for performance to do the craft.entries queries several times, especially if you have many entrytypes. Just thought I'd share this in case it's helpful for your usecase.

> **Comments:**
> **username_2**: André, recommend your Preparse plugin! A simple switch conditional `{% switch entry.type %}{% case 'handle1' %}1{% case 'handle2' %}2{% default %}99{% endswitch %}` and you can use `{% set entries = craft.entries.order('myPreparseHandle') %}`.  
> **username_0**: Yep, that works just fine. The order in this case won't change. Thanks for the idea!  

---

### Answer by username_2 on 2015-11-24. Score: 1

You could try it with a MySQL CASE expression in the order parameter:
{% set orderParam = "CASE
    WHEN type = 'handle1' THEN 1
    WHEN type = 'handle2' THEN 2
    WHEN type = 'handle3' THEN 3
    ELSE 99
END" %}

{% set entries = craft.entries.section('blog').order(orderParam) %}

Another untested suggestion is to try and make this dynamic (order similar to CP):
{% set section = craft.sections.getSectionById(2) %}

{% set orderParam = "CASE" %}

{% for entryType in section.getEntryTypes() %}
    {% set orderParam = orderParam ~ " WHEN type = '" ~ entryType ~ "' THEN " ~ loop.index %}
{% endfor %}

{% set orderParam = orderParam ~ " ELSE 99 END" %}


---

