dongzan2740 2014-12-26 16:30
浏览 43
已采纳

wp_query有多个自定义字段,返回0结果

In wordpess 4.1 I have the following WP_Query:

$products = new WP_Query( array(
        'post_type'      => 'ys_product',
        'orderby'        => array( 'date' ),
        'order'          => 'DESC',
        'posts_per_page' => 8,
        'meta_query'     => array(
            'relation' => 'AND',
            array(
                'key'   => 'ys_product_status',
                'value' => 'ok'
            ),
            array (
                'key'   => 'ys_product_start',
                'value' => date('Ymd'),
                'compare' => '>='
            ),
            array (
                'key'   => 'ys_product_end',
                'value' => date('Ymd'),
                'compare' => '<='
            )
        )
    ) );

And on my DB I have two items with post_type 'ys_product', and with post_meta like this:

Item 1

  • meta_key ys_product_status = 'ok'
  • meta_key ys_product_start = '20141101'
  • meta_key ys_product_end = '20141230'

Item 2

  • meta_key ys_product_status = 'ok'
  • meta_key ys_product_start = '20141101'
  • meta_key ys_product_end = '20150131'

The result of date('Ymd') today is '20141226', which seems clearly between those boundaries.

But if I search only filtering by 'ys_product_status' == 'ok'; I get my two items. But when I add my two other meta, it wont return any results at all.

The contents of $products->request are:

SELECT SQL_CALC_FOUND_ROWS  ysls_posts.ID
 FROM ysls_posts 
 INNER JOIN ysls_postmeta ON ( ysls_posts.ID = ysls_postmeta.post_id )
 INNER JOIN ysls_postmeta AS mt1 ON ( ysls_posts.ID = mt1.post_id )
 INNER JOIN ysls_postmeta AS mt2 ON ( ysls_posts.ID = mt2.post_id )
WHERE 1=1
 AND ysls_posts.post_type = 'ys_product'
 AND (ysls_posts.post_status = 'publish' OR ysls_posts.post_status = 'private')
 AND ( 
  ( ysls_postmeta.meta_key = 'ys_product_status' AND CAST(ysls_postmeta.meta_value AS CHAR) = 'ok'  ) 
   AND 
  ( mt1.meta_key = 'ys_product_start' AND CAST(mt1.meta_value AS CHAR) >= '20141226' ) 
   AND 
  ( mt2.meta_key = 'ys_product_end' AND CAST(mt2.meta_value AS CHAR) <= '20141226' )
 ) GROUP BY ysls_posts.ID  LIMIT 0, 8

The post_meta contents for the two posts I'm targeting: item 1item 2

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duanchu9914 2014-12-26 17:08
    关注
    array (
    'key'   => 'ys_product_start',
    'value' => date('Ymd'),
    'compare' => '>='
    ),
    array (
    'key'   => 'ys_product_end',
    'value' => date('Ymd'),
    'compare' => '<='
    )
    

    here: ys_product_start >= date('Ymd') AND ys_product_end <= date('Ymd') its mean a row must met the both conditions but no one can

    Row 1 NOT  20141101 >= 20141226 AND 20141230 <= 20141226 // false AND  false = false
    meta_key ys_product_start = '20141101'
    meta_key ys_product_end = '20141230'
    
    Row 2 NOT 20141101 >= 20141226 AND 20150131 <= 20141226 // false AND false = false
    
    meta_key ys_product_start = '20141101'
    meta_key ys_product_end = '20150131'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分