简体   繁体   中英

WordPress WP_Query comparing dates in custom field

I am trying to run a WP_Query in WordPress to retrieve posts dated after today using a custom field. My key _mcd_event_date_end has values stored in the m/d/Y format (02/16/2016).

$args = array(
'order' => 'DESC',
'posts_per_page' => -1,
'post_type' => 'mcdevent'
'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => '_mcd_event_date_end',
        'value'   => date("m/d/Y"), 
        'compare' => '>=',
    ),
    array(
        'key' => '_mcd_event_type',
        'value'   => 'Other Event',
        'compare' => '=',
    ),
),
);
$other_events = new WP_Query($args);
wp_reset_postdata();

This is the code I am running, and it works for dates posted this year, but not prior. Meaning if I have a post with the _mcd_event_date_end key having a value of something like 4/16/2016 it will show up and if it is something like 2/12/2016 it will not show up. But then anything from 12/31/2015 and before also shows up.

Thanks for your help and please let me know if there's any more information I can provide.

You should be using "date" for the custom field type:

array(
    'key' => '_mcd_event_date_end',
    'value' => date('Y-m-d H:i:s'),
    'compare' => '>=',
    'type' => 'DATE'
),

But I think the data must be in "YYYY-MM-DD" format for it to work.

http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM