Wordpress ajax 通过组合多个分类来过滤帖子
Wordpress ajax filter posts by combining multiple taxonomies
我正在使用 ajax.
为 Wordpress 中的 post 网格开发过滤器
它从根本上说是一个整体,但我正在尝试使用多个分类法进行过滤。但它并没有结合分类法来优化搜索,例如用 'a' 和 'b'
标记的 posts
它只是显示所有带有标签 'a' 和标签 'b'
的 post
$args = array(
'post_type' => 'projects',
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'] // ASC or DESC
);
if( isset($_POST['multi_subject']) && !empty($_POST['multi_subject']) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['multi_subject']
)
);
}
if( isset($_POST['multi_style']) && !empty($_POST['multi_style']) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'styles',
'field' => 'id',
'terms' => $_POST['multi_style']
)
);
}
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo '<a href="'. get_permalink() .'" data-author="'. get_the_author() .'" data-tier="'. get_author_role() .'">';
echo '<h2>' . $query->post->post_title . '</h2>';
echo '<div>'. the_post_thumbnail() .'</div>';
echo '</a>';
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
我确定使用 isset 然后设置 args 很简单,但我想不通。
您当前的税务查询不允许同时检查这两个分类,因为如果设置了这两个分类,您的第二次税务检查将覆盖第一个。为了允许两者都需要附加它们......将你的 tax_query
重构为这样的东西:
// start with an empty array
$args['tax_query'] = array();
// check for first taxonomy
if( isset($_POST['multi_subject']) && !empty($_POST['multi_subject']) ) {
// append to the tax array
$args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['multi_subject']
);
}
// check for another taxonomy
if( isset($_POST['multi_style']) && !empty($_POST['multi_style']) ) {
// append to the tax array
$args['tax_query'][] = array(
'taxonomy' => 'styles',
'field' => 'id',
'terms' => $_POST['multi_style']
);
}
我正在使用 ajax.
为 Wordpress 中的 post 网格开发过滤器它从根本上说是一个整体,但我正在尝试使用多个分类法进行过滤。但它并没有结合分类法来优化搜索,例如用 'a' 和 'b'
标记的 posts它只是显示所有带有标签 'a' 和标签 'b'
的 post$args = array(
'post_type' => 'projects',
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'] // ASC or DESC
);
if( isset($_POST['multi_subject']) && !empty($_POST['multi_subject']) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['multi_subject']
)
);
}
if( isset($_POST['multi_style']) && !empty($_POST['multi_style']) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'styles',
'field' => 'id',
'terms' => $_POST['multi_style']
)
);
}
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo '<a href="'. get_permalink() .'" data-author="'. get_the_author() .'" data-tier="'. get_author_role() .'">';
echo '<h2>' . $query->post->post_title . '</h2>';
echo '<div>'. the_post_thumbnail() .'</div>';
echo '</a>';
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
我确定使用 isset 然后设置 args 很简单,但我想不通。
您当前的税务查询不允许同时检查这两个分类,因为如果设置了这两个分类,您的第二次税务检查将覆盖第一个。为了允许两者都需要附加它们......将你的 tax_query
重构为这样的东西:
// start with an empty array
$args['tax_query'] = array();
// check for first taxonomy
if( isset($_POST['multi_subject']) && !empty($_POST['multi_subject']) ) {
// append to the tax array
$args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['multi_subject']
);
}
// check for another taxonomy
if( isset($_POST['multi_style']) && !empty($_POST['multi_style']) ) {
// append to the tax array
$args['tax_query'][] = array(
'taxonomy' => 'styles',
'field' => 'id',
'terms' => $_POST['multi_style']
);
}