3

I realise this has been asked before, but this feature seems to have been added in v4.1 and the accepted answers aren't helpful (they're pre-4.1).

How can I "chain" meta queries with ANDs and ORs? I've found this Update on Query improvements in 4.1 but I can't seem to get the syntax correct for my application.

I'd like to query posts where flagged is not 1 AND (expiry_date is in the future OR blank)

I've tried re-ordering the arrays and 'relations' without effect (there are definitely posts in the database which match this query).

The date part of the query works fine, but the addition of the flagged query returns no posts.

'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'   => '='
        )
    )
)
5
  • 1
    Just having flagged there means you're going to search posts only with a meta key flagged. Are you sure you weren't looking for 'compare' => 'NOT EXISTS' ?
    – kovshenin
    Nov 15, 2016 at 16:02
  • All posts have a meta entry flagged, but some posts may not have this set (or set to NULL) while others will be set to 1. Nov 15, 2016 at 16:08
  • can you var_dump the SQL output with and without the "flagged" meta query? (this would be in the $wp_query->request value).
    – Jen
    Nov 15, 2016 at 16:16
  • Solved: flagged was initiated to NULL which seemly prevented the query from working. Setting to 0 and the query works as expected. Nov 15, 2016 at 16:24
  • Glad you were able to solve it! Would you please post the solution as an answer and come back in a couple of days and accept it? This keeps the unanswered queue clear and makes the solution more visible to other users. Nov 15, 2016 at 17:55

1 Answer 1

4

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.

1
  • life saver. Was stuck at this
    – melvin
    Jun 4, 2020 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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