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.

2 Answers
2

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 );

Leave a Reply

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