SOLVED: Post Loop Meta Query by ACF Date Field

I’m creating an ‘events’ page that has dates to it. Is it possible to use the meta query to only show posts that are the same date or newer than the current date? I think I have it close, but not quite sure what to put into the meta key.
image

2 Likes

Hi Lyle,

it should be possible if the dates are saved using a comparable format, like “Y-m-d” or the UNIX timestamp in seconds „U".

The meta key should be “date_of_event” and the meta value the current date “{current_date:Y-m-d}” or “current_date:U”.

It is important to use the same date format for comparison.

Best regards,
timmse

1 Like

Hey @ocbroadband do you mange to solve the problem? I have a similar problem, i have tried the solution that @timmse proposed but it doesn’t work.

This is what I put in, but it still doesn’t seem to work.


Hi Lyle,

please change the meta key to “acf_date_of_event” (without quotes and curly braces) and let me now if it works :slight_smile:

So, I actually just put “date_of_event” to match the ACF field name, and it appears to have worked. I am presuming that ‘Date’ should be the type though, correct? I noticed in another example, someone used numeric, but not sure why.

So, my posts show up, however, it doesn’t appear to filter correctly. I shows ‘all’ the entries regardless of the date, of which I’m using of course, current date as to determine showing or not. Couple screen shots below. If I pick greater than or equal, all the posts show up, if I pick lesser than or equal, they all disappear. Even though I have a test entry that is beyond todays date. I would expect either 2 or only 1 depending on if its set to greater or lesser than, no?




Hi Lyle,
I’ve tried it myself, and it worked (at least I think it did) with the following settings:

ACF Settings (the most important thing is the return format)

Query Loop Settings

The only problem that comes up is the returned date format, that isn’t that much readable - but no problem, you can simply reformat it with the following PHP:

<?php
  $date = get_field('date');
  echo DateTime::createFromFormat("Ymd", $date)->format('m/d/Y');
?>

So instead of using a basic text element or whatever, use the code block to render your date in the format of your choice.

Best regards,
timmse

1 Like

@timmse, Works great! Thank you! Still in the dark sometimes on why things work the way they do on the queries… Sometimes, they just don’t make sense why they work the way they do, like this one, numeric type vs. date. :expressionless:

1 Like

Hi Lyle,
using the following settings should work as well (and you can get rid of the custom PHP):

The ACF date values are stored as YYYYMMDD in the database (I just looked it up), regardless of which return format you’ve set. Accordingly, the current date must have the same format so they can be compared.

Best regards,
timmse

1 Like

Even better! Thank you. Now, just need that conditional logic inside the page, and this would be just awesome so I can display something else if there is nothing in the loop. :slight_smile:

1 Like

One thing at a time :love_you_gesture:

Thanks for this info, it’s helped me set up my events too. I have another question though:

I’m interested in this too. Seeing if I can get this working from the Toolset DateTime custom field.

I see that ACF is more compatible in areas like this. Would Metabox be similar?