gpt4 book ai didi

php - 具有多个自定义字段的 wp_query 返回 0 个结果

转载 作者:可可西里 更新时间:2023-11-01 08:46:13 25 4
gpt4 key购买 nike

在 wordpess 4.1 中,我有以下 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' => '<='
)
)
) );

在我的数据库中,我有两个 post_type 为“ys_product”的项目,并且 post_meta 是这样的:

项目 1

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

项目 2

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

今天 date('Ymd') 的结果是 '20141226',这显然在这些边界之间。

但是如果我只搜索过滤 'ys_product_status' == 'ok';我得到我的两个项目。但是当我添加另外两个元数据时,它根本不会返回任何结果。

$products->请求的内容是:

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

我定位的两个帖子的 post_meta 内容: item 1 item 2

我做错了什么?

最佳答案

array (
'key' => 'ys_product_start',
'value' => date('Ymd'),
'compare' => '>='
),
array (
'key' => 'ys_product_end',
'value' => date('Ymd'),
'compare' => '<='
)

此处:ys_product_start >= date('Ymd') AND ys_product_end <= date('Ymd') 这意味着一行必须满足两个条件但没有人可以

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'

关于php - 具有多个自定义字段的 wp_query 返回 0 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com