【发布时间】: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