doukanzhuo4297 2018-07-03 14:07
浏览 39

WP_Query:meta_query中的第二个参数被忽略

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.

  • 写回答

1条回答 默认 最新

  • doupi4649 2018-07-03 16:35
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致