3

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?

3

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',
        ),
    )

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.