1

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?

3

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.

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') %}. – carlcs Nov 24 '15 at 23:12
  • Yep, that works just fine. The order in this case won't change. Thanks for the idea! – cbad Nov 24 '15 at 23:43
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" %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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