【发布时间】:2017-06-13 17:07:24
【问题描述】:
我有点卡在这里。
The Build Up,我有几个包含 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);
【问题讨论】:
-
更新了我的问题。