自定义文章类型career按job-country筛选后无法按job-category分组问题
自定义文章类型筛选分组问题
问题背景
开发代码用于展示自定义文章类型career的列表,并按自定义分类法job-category分组。预期功能:
- 默认显示所有
career文章列表 - 页面顶部设两个下拉框,对应自定义分类法
job-category和job-country - 用户选择选项点击“筛选”后,列表按
job-category分组更新
当前问题
筛选功能基本正常,但单独选择job-country时,仅显示符合条件的文章列表,未按job-category分组;单独选job-category或同时选两个分类时,均能按「分类名称+对应文章列表」的格式正常显示。
当前代码
<?php $args = array( 'orderby' => 'date', // we will sort posts by date 'order' => $_POST['date'], // ASC or DESC 'post_type' => 'career', 'posts_per_page' => -1, 'post_status' => 'publish' ); if( isset( $_POST['categoryfilter'] ) && isset ($_POST['taxonomyfilter']) ) { $args['tax_query'][] = array( array( 'taxonomy' => 'job-category', 'field' => 'name', 'terms' => $_POST['categoryfilter'] ), array( 'taxonomy' => 'job-country', 'field' => 'name', 'terms' => $_POST['taxonomyfilter'] ), ); } elseif( !isset( $_POST['categoryfilter'] ) && isset ($_POST['taxonomyfilter']) ) { $args['tax_query'][] = array( 'taxonomy' => 'job-country', 'field' => 'name', 'terms' => $_POST['taxonomyfilter'] ); } elseif( isset( $_POST['categoryfilter'] ) && !isset ($_POST['taxonomyfilter']) ) { $args['tax_query'][] = array( 'taxonomy' => 'job-category', 'field' => 'name', 'terms' => $_POST['categoryfilter'] ); } $query = new WP_Query( $args ); if( $query->have_posts() ) : if(isset($_POST['categoryfilter'])) { echo '<h2 class="category-title">'. $_POST['categoryfilter'] .'</h2>'; } while( $query->have_posts() ): $query->the_post(); ?> <div class="job d-flex align-items-center"> <h3 class="job__title col-12 col-lg"><?php echo $query->post->post_title ; ?></h3> </div> <?php endwhile; wp_reset_postdata(); else : echo '<p class="text-center">Sorry, no openings found :-('; endif; die();
解决方案
问题核心是单独选job-country时,代码未按job-category分组输出,仅直接遍历文章。需先获取符合条件的job-category分类项,再逐个分类查询对应文章。
修改后的代码:
<?php // 获取筛选条件 $country_filter = isset($_POST['taxonomyfilter']) ? $_POST['taxonomyfilter'] : ''; $category_filter = isset($_POST['categoryfilter']) ? $_POST['categoryfilter'] : ''; // 确定要展示的job-category分类 $category_terms_args = array( 'taxonomy' => 'job-category', 'hide_empty' => false, // 仅显示包含文章的分类 ); // 若有分类筛选,仅获取指定分类 if ($category_filter) { $category_terms_args['name'] = $category_filter; } $category_terms = get_terms($category_terms_args); if (!empty($category_terms) && !is_wp_error($category_terms)) { foreach ($category_terms as $term) { // 构建每个分类的查询参数 $args = array( 'orderby' => 'date', 'order' => isset($_POST['date']) ? $_POST['date'] : 'DESC', // 加默认值避免报错 'post_type' => 'career', 'posts_per_page' => -1, 'post_status' => 'publish', 'tax_query' => array('relation' => 'AND'), ); // 添加job-category条件 $args['tax_query'][] = array( 'taxonomy' => 'job-category', 'field' => 'name', 'terms' => $term->name, ); // 添加job-country筛选条件(如果有) if ($country_filter) { $args['tax_query'][] = array( 'taxonomy' => 'job-country', 'field' => 'name', 'terms' => $country_filter, ); } $query = new WP_Query($args); if ($query->have_posts()) { // 输出分类标题 echo '<h2 class="category-title">'. esc_html($term->name) .'</h2>'; // 输出该分类下的文章 while ($query->have_posts()) : $query->the_post(); ?> <div class="job d-flex align-items-center"> <h3 class="job__title col-12 col-lg"><?php echo esc_html(get_the_title()); ?></h3> </div> <?php endwhile; wp_reset_postdata(); } } } else { // 无分类结果时的降级处理:直接查询符合条件的文章 $args = array( 'orderby' => 'date', 'order' => isset($_POST['date']) ? $_POST['date'] : 'DESC', 'post_type' => 'career', 'posts_per_page' => -1, 'post_status' => 'publish', ); if ($country_filter) { $args['tax_query'][] = array( 'taxonomy' => 'job-country', 'field' => 'name', 'terms' => $country_filter, ); } $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) : $query->the_post(); ?> <div class="job d-flex align-items-center"> <h3 class="job__title col-12 col-lg"><?php echo esc_html(get_the_title()); ?></h3> </div> <?php endwhile; wp_reset_postdata(); } else { echo '<p class="text-center">Sorry, no openings found :-('; } } die();
修改说明
- 先获取分类项:无论是否选国家筛选,先获取需展示的
job-category分类(有分类筛选则取指定分类,否则取所有含文章的分类) - 逐个分类查询输出:遍历每个分类,构建包含分类+国家(若有)的查询,输出分类标题和对应文章
- 安全与容错处理:用
esc_html()转义输出内容防XSS;给order参数加默认值,避免未传参报错 - 降级兼容:无分类结果时,直接查询符合条件的文章作为 fallback
内容的提问来源于stack exchange,提问作者Julia Galden
相关产品推荐
相关产品推荐

