<p>I want to modify this query so that the dated entries are sorted in descending order on a custom field called 'startDate'.</p>

<p>Have tried adding <code>.order('startDate')</code> to the end of the <code>craft.entries(query)...</code> but this causes any entry with a startDate to be excluded from the query. </p>

<p>(Maybe it's possible to set the order on just the <code>{% for entry in programmeEntriesDated...</code> loop?)</p>

<pre><code>{% set query = { section: 'programme' } %}
{% set query = category is defined ? query | merge({ relatedTo: category }) : query %}
{% set programmeEntriesDated = craft.entries(query) %}

{# Show programme entries if available #}
{% if programmeEntriesDated|length %}
&lt;div class="flex flex-wrap flex-row-ns"&gt;
  {# DATED ENTRIES #}
  {% for entry in programmeEntriesDated if (
        (entry.startDate|date('Y-m-d') &gt;= now|date('Y-m-d'))
    or  (entry.endDate is not null and entry.endDate|date('Y-m-d') &gt;= now|date('Y-m-d'))
    ) and
        (entry.type != 'old_programme_entry')
    and (entry.isOngoing == 0)
  %}
    {% include "_blocks/event_single.html" with { 'blockWidth' : 'w-100 w-third-ns' } %}
  {% endfor %}

  {# ONGOING ENTRIES  #}
  {% for entry in programmeEntriesDated if (entry.isOngoing) and
        (entry.type != 'old_programme_entry')
  %}
    {% include "_blocks/event_single.html" with { 'blockWidth' : 'w-100 w-third-ns' } %}
  {% endfor %}
&lt;/div&gt;
</code></pre>

CC BY-SA 4.0
2018-11-12T12:48:03.657Z
Your use of the order parameter is totally correct. Entries without a value in that field won't be excluded, they'll just appear last. Your first loop won't show them because of the condition (which is a bit strange because you are comparing strings with each other. That may cause unexpected behavior. I would suggest to compare the DateTime)
Robin Schambach
CC BY-SA 4.0
2018-11-12T12:56:07.737Z
Ahh, I don't have a good grasp of how dates work in Twig, thanks for pointing that out Robin.

I just realised a hacky solution, I can reverse the first for loop with `for entry in programmeEntriesDated|reverse `

EDIT: Not really a true solution, as the events still aren't correctly sorted by date)
James Greig
CC BY-SA 4.0
2018-11-12T12:58:25.507Z
Or just re-order them correctly from the beginning with `ASC` or `DESC` you can compare Date objects directly with `entry.date >= now` PHP will compare the timestamps in your DateTime objects which is more accurate than strings
Robin Schambach
CC BY-SA 4.0
2018-11-12T13:27:36.897Z
I can do that inside the original query?
James Greig
CC BY-SA 4.0
2018-11-12T14:09:40.253Z
Can't use entry.date because I want to search on the custom startDate field not the post date (I think...)
James Greig
CC BY-SA 4.0
2018-11-12T14:13:13.367Z
Then use `entry.startDate`.. It was just an example since there is no `date` property for an entry. And yes you have the full power of sql in your order by. You can even include `if` conditions in it https://stackoverflow.com/a/11003441/5604055 `order('IFNULL(content.field_startDate, entries.dateCreated')` or whatever you want (again - just an example to demonstrate your possibilities)
Robin Schambach
<datetime><ordering><craft2>
Order entries by custom date field
# Order entries by custom date field

(Asked by: username_0 on 2018-11-12)

I want to modify this query so that the dated entries are sorted in descending order on a custom field called 'startDate'.
Have tried adding .order('startDate') to the end of the craft.entries(query)... but this causes any entry with a startDate to be excluded from the query. 
(Maybe it's possible to set the order on just the {% for entry in programmeEntriesDated... loop?)
{% set query = { section: 'programme' } %}
{% set query = category is defined ? query | merge({ relatedTo: category }) : query %}
{% set programmeEntriesDated = craft.entries(query) %}

{# Show programme entries if available #}
{% if programmeEntriesDated|length %}

  {# DATED ENTRIES #}
  {% for entry in programmeEntriesDated if (
        (entry.startDate|date('Y-m-d') >= now|date('Y-m-d'))
    or  (entry.endDate is not null and entry.endDate|date('Y-m-d') >= now|date('Y-m-d'))
    ) and
        (entry.type != 'old_programme_entry')
    and (entry.isOngoing == 0)
  %}
    {% include "_blocks/event_single.html" with { 'blockWidth' : 'w-100 w-third-ns' } %}
  {% endfor %}

  {# ONGOING ENTRIES  #}
  {% for entry in programmeEntriesDated if (entry.isOngoing) and
        (entry.type != 'old_programme_entry')
  %}
    {% include "_blocks/event_single.html" with { 'blockWidth' : 'w-100 w-third-ns' } %}
  {% endfor %}

**Tags:** datetime, ordering, craft2

> **Comments:**
> **username_1**: Your use of the order parameter is totally correct. Entries without a value in that field won't be excluded, they'll just appear last. Your first loop won't show them because of the condition (which is a bit strange because you are comparing strings with each other. That may cause unexpected behavior. I would suggest to compare the DateTime)  
> **username_0**: Ahh, I don't have a good grasp of how dates work in Twig, thanks for pointing that out Robin.  I just realised a hacky solution, I can reverse the first for loop with `for entry in programmeEntriesDated|reverse `  EDIT: Not really a true solution, as the events still aren't correctly sorted by date)  
> **username_1**: Or just re-order them correctly from the beginning with `ASC` or `DESC` you can compare Date objects directly with `entry.date >= now` PHP will compare the timestamps in your DateTime objects which is more accurate than strings  
> **username_0**: I can do that inside the original query?  
> **username_0**: Can't use entry.date because I want to search on the custom startDate field not the post date (I think...)  
> **username_1**: Then use `entry.startDate`.. It was just an example since there is no `date` property for an entry. And yes you have the full power of sql in your order by. You can even include `if` conditions in it https://stackoverflow.com/a/11003441/5604055 `order('IFNULL(content.field_startDate, entries.dateCreated')` or whatever you want (again - just an example to demonstrate your possibilities)  

---

## 0 answers

