问题不再相关。。
首先,我考虑了关系或/和的结果,但我认为我应该使用“operator”=>“NOT IN”。我想查询符合多个分类法的结果,而不是显示符合1个税的所有结果。New question has been made.
<?php
function page_content($filters, $is_search = false, $pg = 1, $cat = -1, $post_type) {
if (!$is_search) {
$filters = explode(\',\', $filters);
$query_args = array(
\'posts_per_page\' => 30,
\'post_type\' => $post_type,
\'post_status\' => \'publish\',
\'paged\' => $pg
);
$terms = array();
foreach ($filters as $f) {
if (!empty($f) && is_numeric($f) && $f > 0) {
$terms[] = $f;
}}
if (!empty($terms)) {
if (!is_array($query_args[\'tax_query\'])) $query_args[\'tax_query\'] = array();
$query_args[\'tax_query\'] = array(
\'relation\' => \'OR\', // NOT WORKING, STILL SHOWS \'AND\' RESULTS
array(
\'taxonomy\' => $post_type . \'_filters\',
\'terms\' => $terms,
\'field\' => \'id\',
));
}
$queried_items = new WP_Query($query_args);
} else {
global $wp_query;
$queried_items = $wp_query;
}
foreach ($queried_items->posts as $p) {
$taxonomy = str_replace(\'%#%\', $p->post_type, \'%#%_filters\');
$p->taxonomy = array();
$pfields = array("fields" => "all");
$terms = wp_get_post_terms($p->ID, $taxonomy, $pfields);
$assgined_terms = array();
if ($terms && is_array($terms) && !empty($terms)) {
foreach ($terms as $term) {
if (!in_array($term->parent, $assgined_terms)) {
$p->taxonomy[] = $term;
array_push($assgined_terms, $term->parent);
}} }
}}
最合适的回答,由SO网友:s_ha_dum 整理而成
根据法典:
关系(字符串)—当存在多个内部分类法数组时,每个内部分类法数组之间的逻辑关系。可能的值为“AND”、“OR”。不要与单个内部分类法数组一起使用。
所以你在正确的轨道上,但是你的tax_query
是错误的。relation
是外部的一部分tax_query
数组,而不是顶级查询参数的一部分。
$query_args[\'tax_query\'] = array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => $post_type . \'_filters\',
\'terms\' => $terms,
\'field\' => \'id\',
)
);