WordPress - 2 分类过滤器



>我有点卡在这里。

建立起来,我有几个页面,有 2 个分类法。分支和主题。 每个分类法都有几个关键字。

我现在需要的是。给我"主题"中的所有内容,但只能使用分类分支中的某个关键字。

我像这样尝试过,但这给了我全部来自 Topcis,没有任何来自 Branch 的过滤。

$termArgs = array(
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'page',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'CAT_TOPIC',
'field'    => 'slug'
),
array(
'taxonomy' => 'CAT_BRANCH',
'terms'    => 'solarteur',
'field'    => 'slug'
),
));
$topicTerms = get_terms($termArgs);

更新: 我的问题有误。我不需要页面。我只需要一个来自"CAT_TOPIC"的每个分类法的列表,而不是空的,它还有一个来自"CAT_BRANCH"的某个关键字

我像这样尝试过,但我仍然从 TOPIC 中获得了所有内容的列表。

$termArgs = array(
'taxonomy' => CAT_TOPIC,
'hide_empty' => true,
'parent' => 0,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'CAT_BRANCH',
'terms'    => array('solarteur'),
'field'    => 'slug'
)
)
);

更新 2我现在找到了解决方案。我首先获取所有页面。 之后,我将页面ID与get_the_terms一起使用。 现在我有一个不错的数组,只是在我的案例中有一些重复项。摆脱这个,我就完成了^^

谢谢@Picard

$filter = $_POST['filter'];
$termArgs = array(
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'page',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => CAT_TOPIC,
'field'    => 'term_id',
'terms'    => get_terms( CAT_TOPIC, array( 'hide_empty' => false, 'fields' => 'ids')),
'operator' => 'IN',
),
array(
'taxonomy' => CAT_BRANCH,
'terms'    => $filter,
'field'    => 'slug'
)
)
);
$topicTerms = get_posts($termArgs);
$topicslugs = array();  
foreach($topicTerms as $topicTermKey => $topicTerm):
$currentTerms = get_the_terms( $topicTerm->ID, CAT_TOPIC);
$length = count($currentTerms);
for($x = 0; $x < $length ; $x++){
$topicslugs[] = array($currentTerms[$x] -> slug, $currentTerms[$x] -> name);
}
endforeach;
//remove duplicates
$topicslugs = array_unique($topicslugs, SORT_REGULAR);
$topicslugs = array_filter($topicslugs);
$topicslugs = array_values($topicslugs);

这应该有效:

$termArgs = array(
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'page',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'CAT_TOPIC',
'field'    => 'term_id',
'terms'    => get_terms( 'CAT_TOPIC', array( 'hide_empty' => false, 'fields' => 'ids')),
'operator' => 'IN',
),
array(
'taxonomy' => 'CAT_BRANCH',
'terms'    => array('solarteur'),
'field'    => 'slug'
)
)
);

最新更新