Meta Query with date and time on the same Day before given time

I have a metaquery for a custom post type “events”.
Their start date is stored as a meta field in this format:

2016-02-05 19:00:00

These are my query args:

$args = array(
    "posts_per_page" => 12,
    "paged" => $paged,
    "post_type" => array(
        "event",
        "post"
    ) ,
    "post_status" => "publish",
    "meta_key" => "_thumbnail_id",
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'event_start',
            'value' => date("Y-m-d H:i:s") ,
            'compare' => '>=',
            'type' => 'DATE'
        ) ,
        array(
            'key' => 'event_start',
            'compare' => 'NOT EXISTS'
        )
    ) ,
    array(
        'key' => 'foreign_language',
        'value' => 0
    ) ,
    array(
        'key' => 'hide_from_most_recent',
        'value' => 0
    )
);

The problem: The current date + time is: 2016-02-05 12:29:16

This query should return the post with this meta value: 2016-02-05 19:00:00

Yeterday (one day before this date) it worked. Today it doesn’t.
Means:If the day is the same but hour and minute are bigger than now, it does not work, as it should. It seems, that the hour, minute and second are beeing ignored.
I guess the issue is caused by the fact, that I have mixed up time & date.

Is that correct?
How should the query look like, that it works?

Many Thanks

1 Answer
1

Found the error:

$args = array(
        "posts_per_page" => 12,
        "paged" => $paged,
        "post_type" => array(
            "event",
            "post"
        ) ,
        "post_status" => "publish",
        "meta_key" => "_thumbnail_id",
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'event_start',
                'value' => date("Y-m-d H:i:s") ,
                'compare' => '>=',
                'type' => 'DATETIME'
            ) ,
            array(
                'key' => 'event_start',
                'compare' => 'NOT EXISTS'
            )
        ) ,
        array(
            'key' => 'foreign_language',
            'value' => 0
        ) ,
        array(
            'key' => 'hide_from_most_recent',
            'value' => 0
        )
    );

It must be DATETIME instead of DATE as comparing type.

Leave a Comment