2

I have an events channel where each entry has three custom date/time fields:

startDate (Date Only)

startTime (Time Only)

endTime (Time Only)

I was originally using this code to set my entries, but it causes problems if they aren't all entered on the same day:

{% set allEntries = craft.entries.section('sparkSchedule').limit(null).orderBy('startDate asc').all() %}

I've seen answers from Craft 2 saying that i could use time(startTime) to sort by just the time portion of my startTime field:

{% set allEntries = craft.entries.section('sparkSchedule').limit(null).orderBy('startDate asc, time(startTime)').all() %}

but when I do that, i get this error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'startTime' in 'order clause'

Am I doing something wrong?

2
  • Why are you separating startDate and startTime into two different fields, rather than enabling "time" on the date field?
    – Roi Agneta
    Commented Feb 14, 2019 at 2:38
  • Client preference
    – artmem
    Commented Feb 14, 2019 at 15:15

1 Answer 1

3

You'll want to change that startTime to field_startTime.

Anything in the orderBy gets passed directly onto SQL and Craft is smart enough to do the replacement for startDate behind the scenes, but columns in methods (like time()) or aggregate functions need to be manually prefixed. This should work:

{% set allEntries = craft.entries.section('sparkSchedule').limit(null).orderBy('startDate asc, time(field_startTime)').all() %}
1
  • This seemed to be working fine using the code in the posted solution, but now, it's suddenly ignoring the custom time, seemingly in favor of the entry post time. Has something changed with Craft?
    – artmem
    Commented Jul 8, 2019 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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