简体   繁体   中英

WP_Query: second parameter in meta_query being ignored

The second meta_query parameter is being ignored in this query.

$query = new WP_Query(array(
        'post_type' => 'events',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key'     => 'event_date',
                'value'   => date('Ymd'),
                'compare' => '>=',
            ),
            array(
                'key'     => 'end_date',
                'value'   => date('Ymd'),
                'compare' => '>=',
            )
        )
    ));

There is no mention of end_date in the resulting SQL

SELECT SQL_CALC_FOUND_ROWS inx9uju_posts.ID
FROM inx9uju_posts
INNER JOIN inx9uju_postmeta ON ( inx9uju_posts.ID = inx9uju_postmeta.post_id )
INNER JOIN inx9uju_postmeta AS mt1 ON ( inx9uju_posts.ID = mt1.post_id )
WHERE 1=1
AND ( inx9uju_postmeta.meta_key = 'event_date' AND ( mt1.meta_key = 'event_date' AND CAST(mt1.meta_value AS SIGNED) > '20180703' ) )
AND inx9uju_posts.post_type = 'events'
AND (inx9uju_posts.post_status = 'publish'
OR inx9uju_posts.post_status = 'acf-disabled'
OR inx9uju_posts.post_status = 'private')
GROUP BY inx9uju_posts.ID
ORDER BY inx9uju_postmeta.meta_value+0 ASC LIMIT 0, 30

I've spent some time checking syntax and referring to the codex but I can't see what is wrong with my code. I can't see any reason why it would be ignoring the end_date part of the meta_query parameters.

UPDATE

In this particular case I was overriding the meta_query part of the query in a separate function which was hooking into pre_get_posts . Once I updated that function, the issue was resolved.

If this is stored as an AFC, you must use the ACF syntax of Ymd (20180703).

You must also realise that WordPress can detect the BETWEEN meta_compare to detect two dates as such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM