How to query using a combination of custom_field values?

Since WordPress 4.1 it is possible to build nested meta, date and tax queries. I’ve not tested it but the code bellow should work. Also, I recommend to replace PHP date() function with current_time from WordPress to get the current date taking in account the blog time zone configuration:

After the chat with you, it seems that you just need to use “DATE” as type argument.

$since = current_time('Y-m-d');
$args = array(
        'meta_key'          => 'bjab_event_date_start_id',
        'category_name'     => 'private',
        'order'             => 'ASC',
        'orderby'           => 'meta_value',
        'meta_query'        => array(
            'relation' => 'OR',
            array(
                //event_date_start_id greater or equal to $since
                'key' => 'bjab_event_date_start_id',
                'value' => $since,
                'type' => 'DATE',
                'compare' => '>='
            ),
            array(
                'key' => 'bjab_event_date_end_id',
                'value' => $since,
                'type' => 'DATE',
                'compare' => '>='
            )
        )
    );

$query = new WP_Query( $args );

Leave a Comment