Skip to main content
2 of 3
updated with more info
Stephen
  • 971
  • 3
  • 12
  • 32

Meta compare with date (stored as string) not working

I have a custom post type that has a number of meta boxes in it. One of these is an expiration_date field that stores its info in the db as a string formatted like

Feb 1, 2017

I am trying to write a query that will filter out those items that have a date in the past, only showing future ones. I have tried several versions of the following query, and none work:

$myquery = array (
'post_type' => $cpt,
    'numberposts'=>-1,
    'meta_key' => 'expiration_date',
    'meta_value' => date('M j, Y'),
    'meta_compare' => '<'
    ),
 );

The query returns all posts regardless of date, or zero posts if I reverse the carot (<)

I know that that in order to use date fields WP expects it to be in a different format (YYYY-MM-DD) but this is not how this app was built (by someone else), so the format above is what I am stuck with.

So the question is: Is there a way to convert the datetime BEFORE the compare? If I pull the field out by itself in any record, I can convert it (using strtotime), but of course I am trying to filter out values based on date before display so I have the right number in the set to work with at the start.

Stephen
  • 971
  • 3
  • 12
  • 32