1

I want to put the index of section in order by two fields: featured and startDate. Basically, I want the featured entries to rise to the top of the index, and then within the featured posts I want them to be sorted by startDate field. Below the featured entries, I want the other entries to also be sorted by startDate.

I tried entries.order('featured desc').order('startDate desc') but, as I expected, it simply ordered by startDate because that method executed after ordering by featured.

Is there any way to achieve what I'm tring to do?

3

This should get you the desired results:

{% for entry in craft.entries.section('yoursection').order('startDate desc, featured desc') %}

    {# Entry Code #}

{% endfor %}

If for any reason that doesn't achieve exactly what it is you are looking for, you could always break it up into multiple loops, 1 for featured, and 1 for not featured. It's not as clean as the above solution but should also solve the issue:

This will use 2 loops, one for the featured and 1 for the not featured, assuming the 'featured' field is a lightswitch.

{% for entry in craft.entries.section('yoursection').search('featured:1').order('startDate desc') %}

    {# Entry Code #}

{% endfor %}

and

{% for entry in craft.entries.section('yoursection').search('featured:not 1').order('startDate desc') %}

    {# Entry Code #}

{% endfor %}

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.