我正在使用 ajax 為 Wordpress 中的后網格開發過濾器。
它基本上是一個整體,但我正在嘗試使用多種分類法進行過濾。但不是結合分類法來優化搜索,例如用“a”和“b”標記的帖子
它只是顯示所有帶有標簽“a”和標簽“b”的帖子
$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 很簡單,但我無法弄清楚。
uj5u.com熱心網友回復:
您當前的稅務查詢不允許檢查兩種分類法,因為如果兩者都設定了,您的第二次稅務檢查將覆寫第一次。為了允許兩者,您需要附加它們...將您重構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']
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/434134.html
