Meta query with multiple logic (AND / OR)

The correct syntax for performing the AND OR is as follows:

'meta_query'    => array(
    'relation' => 'AND',
    array( 
        'key'     => 'flagged',
        'value'   => '1',
        'compare' => '!='
    ),
    array(
        'relation' => 'OR',
        array(
            'key'       => 'expiry_date',
            'value'     => date('Y-m-d',strtotime("today")),
            'compare'   => '>=',
            'type'      => 'DATETIME',
        ),
        array( //has no date
            'key'       => 'expiry_date',
            'value'     => '',
            'compare'   => '='
        )
    )
)

As a side note, it seems that post meta which are set to NULL (eg flagged) will not be returned by this query. Setting to any other value (such as 0) resolves this issue.

Leave a Comment