【问题标题】:Wordpress - 2 Taxonomies FilterWordpress - 2 分类过滤器
【发布时间】: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);

【问题讨论】:

  • 更新了我的问题。

标签: php wordpress


【解决方案1】:

这应该可行:

$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'
            )
    )
);

【讨论】:

  • 谢谢你。但我认为,我的问题有误。我不需要页面。我只需要一个来自“CAT_TOPIC”的每个分类的列表,而不是空的,它也有来自“CAT_BRANCH”的某个关键字。
  • 用一个不起作用的新代码片段更新了我的问题 :-)
  • 分类没有分配其他分类,但一般分配给帖子。因此,您可以检查具有这些分类法的帖子和页面。
  • 谢谢@Picard 船长,我在开篇文章中发布了我的解决方案。我必须“过滤”页面,所以我只能得到我需要的分类法。祝你有美好的一天°
猜你喜欢
  • 2014-11-19
  • 2015-04-17
  • 2019-08-11
  • 2015-02-25
  • 2014-09-30
  • 2022-01-03
  • 2023-03-05
  • 1970-01-01
  • 2018-02-26
相关资源
最近更新 更多