Just like the title suggests, I’ve spent the last several hours struggling with
meta_query to return CPTs based on some
meta_value. Let me explain my setup.
I’m writing a plugin that stores data from a
custom_post_type for gym classes with a handful of custom fields put into place using ACF. One of those custom fields is
gym_class_date which contains the output from a jQuery
datetime picker. Here’s a look at my broken code:
When I ran the code, the resulting output was… nothing. So what gives?
I burned an entire Sunday scratching my head over this and I finally figured it out. Here was my problem:
ACF always stores
datetime values in an EXTREMELY specific way:
'YYYY-MM-DD HH:ii:ss'. And I mean specific. Even though earlier in my code, I defined
$today = date('Ymd H:i:s');… it was just wrong enough to break literally everything. Turns out,
20210116 16:37:24 does NOT equal
Adding some simple dashes to my
$today = date('Y-m-d H:i:s'); declaration made everything magically start working again.
That’s gonna do it for this post, I know it’s a bit of a short one, but it took me FOREVER to figure out this stupid detail. Note to self, and hopefully to you, when you’re comparing two
datetime values with a
meta_query, for Pete’s sake, make absolutely sure that your format is perfectly the exact same as the format coming from the database. Otherwise, none of your comparison operators will work.