Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

That was a very dense title.

I have a custom post type "event" which has a Date/Time picker field "event_date" thanks to the Advanced Custom Fields plugin. This Date/Time picker saves a UNIX timestamp to the database. I'm trying to use WP_Query to get all events that are today or in the future. This is the code that I have right now:

$args = Array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'event_date',
    'orderby'           => 'meta_value_num',
    'order'             => 'ASC',
    'meta_query'        => array(
                'key'       => 'event_date',
                'compare'   => '>=',
                'value'     => intval(strtotime(date('Y-m-d'))),
                'type'      => 'numeric'
    ),
);

$query = new WP_Query( $args );

It's giving me all the events, past and future.

I realize that the timestamps are stored as strings in the database so 'compare' => '>=' normally wouldn't work, but what what I've read on Google and in the Codex that 'type' => 'numeric' should cast the string to an integer and allow it to be compared with my value of this morning at midnight. Unfortunately that doesn't seem to be working and I don't understand why.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Try an array of arrays in your meta query.

$args = Array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'event_date',
    'orderby'           => 'meta_value_num',
    'order'             => 'ASC',
    'meta_query'        => array(
         array(
                'key'       => 'event_date',
                'compare'   => '>=',
                'value'     => intval(strtotime(date('Y-m-d'))),
                'type'      => 'numeric'
         )
    ),
);

$query = new WP_Query( $args );
share|improve this answer
    
Thanks a lot, this is also work for me for Unix Time. (You save my lot time) –  Ajay Patel yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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