0

I have a custom field called date_from. Dates are stored in the yymmdd format (eg 20131104). When I use a meta_query to find posts in a specific date range the query works as expected, but when I use a meta_query to find posts which have a date_from which is greater than the specified value it doesn't. That means; it doesn't return posts while I know for sure that it should. I can't figure out what I'm doing wrong...

This is working:

$month = date('m');
$start_date = date( 'Y' . $month . '01' );
$end_date = date( 'Y' . $month . 't' );

$meta_query = array(
    'key'       => 'date_from',
    'value'     => array($start_date, $end_date),
    'compare'   => 'BETWEEN',
    'type'      => 'DATE'
);

args = array(
    'post_type'         => 'events',
    'post_status'       => 'publish',
    'posts_per_page'    => 20,
    'paged'         => $paged,
    'meta_query'        => array($meta_query),
    'meta_key'      => 'date_from',
    'orderby'           => 'meta_value',
    'order'         => 'ASC',
);
$query = new WP_Query($args);

And this is not:

$today = date("Ymd");

$meta_query = array(
    'key'       => 'date_from',
    'value'     => $today,
    'compare'   => '>=',
    'type'      => 'DATE' // I've also tried type => 'NUMERIC'
);

args = array(
    'post_type'         => 'events',
    'post_status'       => 'publish',
    'posts_per_page'    => 20,
    'paged'         => $paged,
    'meta_query'        => array($meta_query),
    'meta_key'      => 'date_from',
    'orderby'           => 'meta_value',
    'order'         => 'ASC',
);
$query = new WP_Query($args);

And it does work when I use:

$args = array(
    'post_type'         => 'events',
    'post_status'       => 'publish',
    'posts_per_page'    => 20,
    'paged'             => $paged,
    'meta_key'          => 'date_from',
    'meta_value'        => $today,
    'meta_compare'      => '>',
    'orderby'           => 'meta_value',
    'order'             => 'ASC'
);

Why is that? I can't use $meta_query = array(...) to find posts which have a date greater than a specified value? Seems really odd because when I use exactly the same values directly in $args it does work.

  • 1
    Did you search for this? There are several Q&A's on here concerning (meta_)queries and dates. – Nicolai Nov 4 '13 at 14:37
  • 2
    It looks like you ain't got debugging activated. Please enable debugging and file an edit so we can look at your error messages. – kaiser Nov 4 '13 at 14:54
  • 2
    @EricHolmes: $query = new WP_Query($args); printf( '<pre>%s</pre>', $query->request ); might be useful in this case ... – birgire Nov 4 '13 at 14:57
  • 2
    @birgire; not a copy/paste problem :-). @kaiser: I've left out $paged since it wasn't relevant to the problem and I have debugging enabled. But I have to confess something here (what's the emoticon for shame?); $meta_query was built based on input from a form. Somewhere I've mixed up an if statement which builds the $meta_query and now it's working. Oops! Sorry guys but thanks a lot of the useful input! I'll make sure I'll describe my questions better in the future. – eskimo Nov 4 '13 at 15:04
  • 2
    @joostvanhoof No problem. Sometimes you need to just write things down to get aware of the issues. Two thirds of all questions I ever wanted to write have been solved during writing and have never been asked :) – kaiser Nov 4 '13 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.