我确实有一个子页面,我想在其中展示和过滤我客户公司的职位发布。
过滤基于两个分类法:
什么有效:
什么不起作用:
在我的代码的当前版本中,当我按国家/地区过滤结果时,它会以这种方式显示结果:
JOB CATEGORY 1
post
post
JOB CATEGORY 2
Sorry, no openings found
JOB CATEGORY 3
post
post
post
所以我只想删除整个职位类别 2(并且不显示有关缺乏空缺职位的信息)。我确实有这样的印象:我的代码的问题与taxquery if 语句的部分有关。但我无法诊断那里出了什么问题:/
代码:
function multiple_filter_function()
{
$list_custom_taxonomy = get_terms(array(
'taxonomy' => 'job-category',
'name' => $_POST['categoryfilter'],
'hide_empty' => true
));
foreach ($list_custom_taxonomy as $custom_single_term) {
echo '<h2 class="category-title">' . $custom_single_term->name . '</h2>';
$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'
);
/* taxquery if statements */
if (isset($_POST['categoryfilter']) && isset($_POST['taxonomyfilter'])) {
$args['tax_query'][] = array(
'relation' => 'AND',
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(
'relation' => 'AND',
array(
'taxonomy' => 'job-category',
'field' => 'name',
'terms' => $custom_single_term->name,
),
array(
'taxonomy' => 'job-country',
'field' => 'name',
'terms' => $_POST['taxonomyfilter']
)
);
} elseif (isset($_POST['categoryfilter']) && !isset($_POST['taxonomyfilter'])) {
$args['tax_query'][] = array(
array(
'taxonomy' => 'job-category',
'field' => 'name',
'terms' => $_POST['categoryfilter']
)
);
}
/* END of taxquery if statements */
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
echo $query->post->post_title;
endwhile;
wp_reset_postdata();
else :
echo '<p class="text-center">Sorry, no openings found</p>';
endif;
}
die();
}
add_action('wp_ajax_myfilter_2', 'multiple_filter_function');
add_action('wp_ajax_nopriv_myfilter_2', 'multiple_filter_function');
正如 CBroe 在评论中所建议的那样。 IMO,这是最可行的解决方案。
function multiple_filter_function()
{
$list_custom_taxonomy = get_terms(array(
'taxonomy' => 'job-category',
'name' => $_POST['categoryfilter'],
'hide_empty' => true
));
foreach ($list_custom_taxonomy as $custom_single_term) {
$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'
);
/* taxquery if statements */
if (isset($_POST['categoryfilter']) && isset($_POST['taxonomyfilter'])) {
$args['tax_query'][] = array(
'relation' => 'AND',
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(
'relation' => 'AND',
array(
'taxonomy' => 'job-category',
'field' => 'name',
'terms' => $custom_single_term->name,
),
array(
'taxonomy' => 'job-country',
'field' => 'name',
'terms' => $_POST['taxonomyfilter']
)
);
} elseif (isset($_POST['categoryfilter']) && !isset($_POST['taxonomyfilter'])) {
$args['tax_query'][] = array(
array(
'taxonomy' => 'job-category',
'field' => 'name',
'terms' => $_POST['categoryfilter']
)
);
}
/* END of taxquery if statements */
$query = new WP_Query($args);
if ($query->have_posts()) :
echo '<h2 class="category-title">' . $custom_single_term->name . '</h2>';
while ($query->have_posts()) :
$query->the_post();
echo $query->post->post_title;
endwhile;
wp_reset_postdata();
endif;
}
die();
}
add_action('wp_ajax_myfilter_2', 'multiple_filter_function');
add_action('wp_ajax_nopriv_myfilter_2', 'multiple_filter_function');