gpt4 book ai didi

php - 在 wp_Query 的 $args 中执行错误的关系值?

转载 作者:行者123 更新时间:2023-11-29 20:28:52 24 4
gpt4 key购买 nike

我正在尝试从数据库中获取我的自定义插件和帖子类型的数据。我的查询参数应该像

$args = array(
'post_type' => 'products',
'post_status'=> 'publish',
'meta_query' => array(
'relation' => 'OR',
array( 'key'=>'product_commercial',
'value'=>'on',
'compare'=>'='
),
array( 'key'=>'product_exterior',
'value'=>'on',
'compare'=>'='
)
)
);
$search_query = new WP_Query( $args );

但我正在尝试动态添加元键值,例如:

$inner_arrays=array();
$count = 0;
foreach($values as $value){
if($value){

$inner_arrays[$count]['key'] .= $value;
$inner_arrays[$count]['value'] .= 'on';
$inner_arrays[$count]['compare'] .= '=';
$count++;
}
}

$args = array(
'post_type' => 'products',
'post_status'=> 'publish',
'meta_query' => array(
'relation' => 'OR',
$inner_arrays
)
);

//值是一些随机值(比如从数据库获取)。

现在,当我使用 echo "<pre>Last SQL-Query: {$search_query->request}".'<br/>'; 打印查询时

显示

    Last SQL-Query: SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )  INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1  AND ( 
(
( wp_postmeta.meta_key = 'product_commercial' AND CAST(wp_postmeta.meta_value AS CHAR) = 'on' )
**AND**
( mt1.meta_key = 'product_framed' AND CAST(mt1.meta_value AS CHAR) = 'on' )
**AND**
( mt2.meta_key = 'product_horizontal' AND CAST(mt2.meta_value AS CHAR) = 'on' )
)
) AND wp_posts.post_type = 'products' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

问题:我正在使用“relation => OR”,但在 sql 查询中得到“AND”。我哪里做错了?

最佳答案

$args['meta_query'] 被分配以下内容:

array(
'relation' => 'OR',
$inner_arrays
)

如果你要检查这个,例如通过执行 print_r( $args['meta_query'] );,您会看到:

Array(    [relation] => OR    [0] => Array        (            [0] => Array                (                    key => product_commercial,                    value => on,                    compare => =                ),             [1] => Array                (                    key => product_exterior,                    value => on,                    compare => =                )        ))

换句话说,$inner_arrays 数组本身成为$args['meta_query'] 的子数组。如 Custom Field Parameters 下所述:

Important Note: meta_query takes an array of meta query arguments arrays (it takes an array of arrays) - you can see this in the examples below. This construct allows you to query multiple metadatas by using the relation parameter in the first (outer) array to describe the boolean relationship between the meta queries. Accepted arguments are 'AND', 'OR'. The default is 'AND'.

因此,您将使用 AND 的默认关系组合 $inner_arrays 的元素,然后将 that 的结果与任何其他组合$args['meta_query'] 的成员(没有任何)使用 OR 的显式关系。

您必须将 $inner_arrays 的每个元素附加到 meta_query,或者仅设置 $inner_arrays['relation'] = 'OR'.

关于php - 在 wp_Query 的 $args 中执行错误的关系值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39166571/

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