我使用所有复选框创建了一个过滤器,以按标签显示特定帖子。我希望返回的帖子的结果包含基于复选框的所有标签。我可以将tax_query设置为relation => 'AND'但如果它没有得到所有变量,它什么都不返回。应该允许用户检查他们想要的少量或多个标签过滤器,但结果帖子应该包含所有标签。这是进行处理的函数..
function CaseStudiesAjaxFunction() {
global $post;
$advertiser = isset( $_GET["advertiser"] )? $_GET["advertiser"] : '';
$agency = isset( $_GET["agency"] )? $_GET["agency"] : '';
$automotive = isset( $_GET["automotive"] )? $_GET["automotive"] : '';
$education = isset( $_GET["education"] )? $_GET["education"] : '';
$financial = isset( $_GET["financial"] )? $_GET["financial"] : '';
$retail = isset( $_GET["retail"] )? $_GET["retail"] : '';
$travel = isset( $_GET["travel"] )? $_GET["travel"] : '';
$search = isset( $_GET["search"] )? $_GET["search"] : '';
$social = isset( $_GET["social"] )? $_GET["social"] : '';
$smartpath = isset( $_GET["smartpath"] )? $_GET["smartpath"] : '';
$halogen = isset( $_GET["halogen"] )? $_GET["halogen"] : '';
$kenshoo = isset( $_GET["kenshoo"] )? $_GET["kenshoo"] : '';
$techvalidate = isset( $_GET["techvalidate"] )? $_GET["techvalidate"] : '';
$infinityawards = isset( $_GET["infinityawards"] )? $_GET["infinityawards"] : '';
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $advertiser
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $agency
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $automotive
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $education
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $financial
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $retail
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $travel
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $search
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $social
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $smartpath
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $halogen
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $kenshoo
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $techvalidate
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $infinityawards
)
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'case-studies'
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
echo '<div class="case-study">';
echo '<div class="case-study-img-container">';
the_post_thumbnail();
echo '</div>';
the_title(); echo '<br>';
the_tags();
echo '</div>';
endwhile; endif;
die();
}
答案 0 :(得分:0)
您正在使用tax_query。
我的建议是将所有这些标记组合成逗号分隔的字符串,然后使用标记参数。
http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
将标记放入数组中以使其更易于处理。如果没有设置值,则不要将值设置为空字符串,而是使用false,以便array_filter()
删除它们。
http://php.net/manual/en/function.array-filter.php
将元素内嵌到字符串中,然后设置标记参数。
$tags = array(
'advertiser' => isset( $_GET["advertiser"] ) ? $_GET["advertiser"] : false,
'agency' => isset( $_GET["agency"] ) ? $_GET["agency"] : false,
);
$tag_string = implode( ',', array_filter( $tags ) );
$args = array(
'post_type' => 'post',
'posts_per_page' => 12
);
if ( ! empty( $tag_string ) ) {
$args['tag'] = $tag_string;
}