I have a custom post type which stores dates in a meta field as

2014-02-22 22:38:00

I can use WP_Query to find posts created in a certain month / year

$args = array(
        'post_type' => 'custom_post',
        'post_status' => 'publish',
        'date_query' => array(array(
            'month' => $month,
            'year'=> $year,
        ),),

    );
    $query = new WP_Query($args);

How can I extend this to search a date meta field? I know you have to add

'meta_query' => array(array(
            'key'     => $key,
            'value'   => $val,
            ...etc...
    ),),

Can I search for custom posts using a date field or do I have to manually filter once I get all the returned custom posts?

1 Answer
1

See the codex re Meta Query.
For example, this would get all posts at least a day old:

$date = date('Y-m-d H:i:s',strtotime("-1 days"));  // 1 day ago

meta_query' => array(
        array(
            'key'       => 'name of your key',
            'value'     => $date,
            'compare'   => '<=',
            'type'      => 'DATETIME',
        ),
    )

Leave a Reply

Your email address will not be published. Required fields are marked *