因此,在这个自定义帖子类型列表页面上有一个属性列表。
我有一个自定义搜索功能,他们可以搜索具有特定分类术语的属性,但也可以搜索多个分类术语。
Question:如果有人选择了两个术语,那么列表中应该显示的唯一属性就是带有term_one
AND
term_two
因此,我尝试了以下方法:
$required_features_slugs = array();
if( isset ( $_GET[\'features\'] ) ) {
$features_slugs = $_GET[\'features\'];
}
$feature = get_terms(\'features\');
$tmp_required = $required_features_slugs;
$property = array(
\'post_type\' => \'properties\',
\'paged\' => $paged,
\'tax_query\' => array(
array(
\'taxonomy\' => \'features\',
\'field\' => \'slug\',
\'terms\' => $tmp_required,
\'operator\' => \'AND\',
)
),
);
URL如下所示:
features%5B%5D=accredited-landlord&features%5B%5D=cellarbasement
现在,由于选择了这两个功能,它应该只返回一个属性,因为只有一个属性具有
term_one
AND
term_two
在一起
然而,我没有得到这个结果,而是得到了4个属性,因为有4个属性。3带term_one
和1个term_two
.
我希望返回1个属性,因为只有一个属性term_once
AND
term_two
在一起
我在查询中是否有什么错误?
Edit: 已更新PHP
文件
$required_features_slugs = array();
if( isset ( $_GET[\'features\'] ) ) {
$required_features_slugs = $_GET[\'features\'];
}
$all_features = get_terms(\'features\');
if( ! empty( $required_features_slugs ) ) {
foreach ( $all_features as $feature ) {
$tmp_required = $required_features_slugs;
if( ! in_array($feature->slug, $required_features_slugs) ) {
array_push( $tmp_required, $feature->slug );
}
$property = array(
\'post_type\' => \'properties\',
\'paged\' => $paged,
\'tax_query\' => array(
\'taxonomy\' => \'features\',
\'field\' => \'slug\',
\'terms\' => $tmp_required,
\'operator\' => \'AND\',
),
);
}
}
Edit #2:下面是
var_dump
到查询:
array(3) { ["post_type"]=> string(10) "properties" ["paged"]=> int(1) ["tax_query"]=> array(4) { ["taxonomy"]=> string(8) "features" ["field"]=> string(4) "slug" ["terms"]=> array(2) { [0]=> string(19) "accredited-landlord" [1]=> string(14) "cellarbasement" } ["operator"]=> string(3) "AND" } }
这是
var_dump
根据条款:
array(2) { [0]=> string(19) "accredited-landlord" [1]=> string(14) "cellarbasement" }
SO网友:birgire
Query Arguments:
如果
$input_terms
是术语段塞的输入数组,那么您应该能够使用(如果我正确理解问题):
$property = [
\'post_type\' => \'properties\',
\'paged\' => $paged,
\'tax_query\' => [
[
\'taxonomy\' => \'features\',
\'field\' => \'slug\',
\'terms\' => $input_terms,
\'operator\' => \'AND\',
]
],
];
我们在税务查询中添加了缺失的数组。
Validation:
我们应该
validate 首先输入术语。
以下是一些示例:
检查允许的最大条款数:
$valid_input_terms_max_count = count( $input_terms ) <= 4;
检查允许的最小条款数:
$valid_input_terms_min_count = count( $input_terms ) >= 1;
检查是否存在输入项slug(假定输入数组非空):
$valid_input_terms_slugs = array_reduce(
(array) $input_terms,
function( $carry, $item ) {
return $carry && null !== term_exists( $item, \'features\' );
},
true
);
我们收集的地方
term_exists()
将所有术语转换为单个布尔值。
我们还可以将输入段塞与预定义的段塞数组进行匹配
Generated SQL:
下面是为中的两个现有术语段塞生成的SQL查询
$input_terms
:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1 AND (
(
SELECT COUNT(1)
FROM wp_term_relationships
WHERE term_taxonomy_id IN (160,161)
AND object_id = wp_posts.ID
) = 2
)
AND wp_posts.post_type = \'properties\'
AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10
希望您可以根据自己的需要进行调整。