【问题标题】:Search & Filter Query Across Multiple Taxonomies in WordPress在 WordPress 中跨多个分类法搜索和过滤查询
【发布时间】:2019-03-11 08:34:56
【问题描述】:

我们有一个自定义帖子类型 hr-priority,它有多个与之关联的自定义分类。这些分类法包含多个术语,每个帖子都可以从这些分类法中分配一对多的术语。我们正在尝试设置一个过滤器,允许访问者通过这些术语的组合在多个分类中搜索/过滤结果。

例如: 帖子“Sample Post”的分类“年份”为“2018”,“组织”分类为“理事会成员”,“国家”分类为“加拿大”。 “Other Sample”帖子中的“年份”为“2016”,“组织”分类为“经理”,“国家”分类为“加拿大”。 帖子“第三篇文章”的分类“年份”为“2018”,“组织”分类为“安全团队”,“国家”分类为“冰岛”。

我们希望能够按所有这些术语过滤结果,所以如果我选择只查看 year=2018 的帖子,我会得到“第三篇帖子”和“示例帖子”,但如果我进一步按 country=canada 过滤我只会得到“Sample Post”(因为过滤器是 year=2018 和 country="canada")。

我正在使用搜索和过滤插件。我尝试使用 archive-hr-priority.php 模板页面获取结果,但结果中根本无法识别该模板。然后我使用了自定义分类模板,例如 taxonomy-year.php,它只使用一个过滤器,所以当我按 year=2018 过滤时,返回的唯一帖子来自 2018 年,但是当我添加了第二个搜索词,例如 country="canada",它返回 country=canada 的所有结果,但包含所有年份。

这是我的搜索和过滤短代码:

echo do_shortcode( '[searchandfilter post_types="hr-priority" fields="year,country"]' );

这是我在自定义分类模板中使用的代码(我为每个分类创建了一个):

if (is_tax() || is_category() || is_tag() ){
        $qObj = get_queried_object();

        $args = array(
            'post_type'=>'hr-priority',
            'tax_query' => array( array(
                  'taxonomy' => $qObj->taxonomy,
                  'field' => 'id',
                  'terms' => $qObj->term_id
                )),
            'post_status'=>'publish',
            'posts_per_page'=>20
        );
    }
    echo($qObj->slug); //just to view the term/s being used
     $wpb_all_query = new WP_Query($args);
    if ( $wpb_all_query->have_posts() ) : ?>
        <?php
        /* Start the Loop */
        while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();

            get_template_part( 'template-parts/post/hr-priorities-post', get_post_format() );

        endwhile;

这是提交 Search&Filter 参数后返回的 URL:

https://exampleurl.org/org/council-member?country=afghanistan&post_types=hr-priority-un

如何让它按多个分类法中的多个术语进行过滤,并仅返回与所有搜索参数匹配的结果?

【问题讨论】:

    标签: php wordpress search plugins filter


    【解决方案1】:

    我不熟悉“搜索和过滤”,但这看起来很容易使用 base WP_Query。您可以使用以下方法跨多个分类进行搜索:

    $tax_query = array(
      'relation' => 'AND'
    );
    
    // get query parameters and compose the condition
    $year = $_GET['year'];
    if (!empty($year)) {
      $cond = array(
        'taxonomy' => 'year',
        'field' => 'slug',
        'terms' => $year,
      ));
      $tax_query[] = $cond;
    }
    // Repeat for other parameters
    
    $args = array(
      'post_type'=>'hr-priority',
      'tax_query' => $tax_query,  
      'post_status'=>'publish',
      'posts_per_page'=>20,
    );
    

    Reference documentation

    【讨论】:

    • 所以,我找到了解决方案。你的回答肯定有帮助,但$_GET 不是我需要的,我必须为我可能查询的每个分类使用get_query_var,然后将每个税务查询放入同一个数组中,但我确实需要有那个'relation' =&gt; 'AND'
    • @calwex718 是的,看看答案修订版。使用$_GET 被认为“更安全”,因为我不知道 S&F 是如何工作的,或者它是否注册了变量。下次我会指出来的。很高兴它正在工作!
    【解决方案2】:

    我通过其他几个问题的一些答案找到了答案(请参阅下面的引文列表)。

    首先,我必须在我可能使用的每个分类法中获取完整的术语列表(这样,如果没有查询一个税,我可以提供一些值,而不是一个空的字符串,对于那个 tax_query 的 'term' =&gt; 值),然后我把它变成了 slugs(下面的第二行),然后我使用 get_query_var() 函数从每个/任何分类法。

        $allStates = get_terms('country', $args = array('hide_empty' => false));
        $state_names = wp_list_pluck( $allStates, 'slug' );
        $country = (get_query_var('country', $state_names));
    

    然后,我将 get_query_var() 的值发送到我的 tax_query 的数组中(我本可以在 $args 数组中完成此操作,但除此之外更易于阅读),但需要使用 @ 987654329@ (as pointed out in another suggested answer) 以确保每个都包含在查询参数中:

          $tax_query = array(
          'relation' => 'AND',
          array(
            'taxonomy' => 'country',
            'field' => 'slug',
            'terms' => $country,
          ),
          array(
            'taxonomy' => 'org',
            'field' => 'slug',
            'terms' => $org,
          ),);
    

    之后就是同一个将军wp_query()

            $args = array(
                'post_type'=>'hr-priority',
                'tax_query' => $tax_query,
                'post_status'=>'publish',
                'posts_per_page'=>20
            );
    //  print_r($tax_query);
         $wp_all_query = new WP_Query($args);
        if ( $wp_all_query->have_posts() ) : ?>
    

    找出解决方案时使用的答案的引文列表(谢谢!): https://wordpress.stackexchange.com/a/35611/146095 https://wordpress.stackexchange.com/a/157167/146095

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2020-12-14
      • 1970-01-01
      • 2019-07-27
      • 2013-05-25
      • 2017-06-17
      相关资源
      最近更新 更多