Ordering Events by Advanced Custom Fields Date & Time Picker Field

While working on the DJ Gigs plugin, at a certain point it became necessary to filter and sort dj gigs by the start date & time using the Advanced Custom Fields Date & Time Picker. This isn’t as easy as it seems. Hours of creative Googling got me all kinds of code snippets that looked promising but in the end didn’t work.

For this example, we need to show all events that are either from today or in the future. So we need to compare our event start date + time with today’s date and time. The answer is deceptively simple however. The main point to remember is that your date + time format has to match the format of today’s date + time that you grab from the server. Once those formats match, the filtering is a piece of cake.

In your ACF Date & Time Picker field settings, make sure you have both ‘Save as timestamp’ and ‘Get field as timestamp’ checked. This makes it much easier to compare and filter by date. I’ll show you later how to format dates to use in your theme.

So now on to our custom loop using WP_Query. First we need to get the current datetime as a unix timestamp:

Then set up our custom event loop with a meta_compare operator:

And that’s it! Again, the main thing to remember is that your datetime formats have to match. There are other WordPress and PHP methods to get the current datetime: date(), getdate(), etc. so whichever method you choose has to match the way your ACF Date & Time Picker saves the datetime in the database. Thus if you don’t select ‘Save as timestamp’ in your Date & Time Picker settings, just echo your date field to see what the format is. If it is an array you can use either strtotime or WordPress’ built-in date_i18n function to output the current time to match.

To use your timestamp ACF Date & Time Picker field data in your theme, use something like this:

So if your event starts on April 1, this will output 2014-04-01. More on WordPress datetime formats.

See All Posts →