Skip to main content
Notice removed Canonical answer required by Stephen
Bounty Ended with gmazzap's answer chosen by Stephen
Tweeted twitter.com/StackWordPress/status/830164315361640448
added 2 characters in body
Source Link
Stephen
  • 971
  • 3
  • 12
  • 32

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 stuckworking 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.

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.

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 working 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.

updated with more info
Source Link
Stephen
  • 971
  • 3
  • 12
  • 32

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.

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 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.

Notice added Canonical answer required by Stephen
Bounty Started worth 50 reputation by Stephen
Source Link
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 (<)