How do we order a query loop by a Time ACF custom field?

Hi folks,

I’ve got an events website I’m building and we need to show events per day, ordered by the time that they start, ascending… so earliest ones first. It sounds simple enough, but the time field seems to not give it the correct number ordering for the query to work as expected. It does things like…

1.00pm, 11.00am, 12.00pm, 2.00pm and so on.

This was a similar issue with the date field on a previous project, and we ended up having to change the format of the date data to be YYYYMMDD then the sorting worked. I don’t seem to be able to get a similar solution for the Time field.

Any ideas how to solve this?
Thanks in advance!

Hey @leef,

as ACF stores a time picker field’s value as H:i:s in the database (no matter what format you chose for the actual output) there shouldn’t be any issue ordering by it.

See ACF documentation:

The value selected can be returned and displayed in different formats but will be saved to the database as ‘H:i:s’. This format is used throughout the WordPress database tables and will allow for straight-foward database querying.

Best,

André

Thanks for the reply André.

I have tried all the various sorting/format output options in the query loop builder, but it will not work as intended. I’ve resorted to adding a new ACF field with a number in it, so I can order by that field instead. 01, 02, 03 etc. This is working ok so far, but will dig deeper into the time sorting after launch.

All the best!