[英]WordPress Custom Taxonomies WP_Query
我正在嘗試創建一個查詢,在該查詢中我可以在自定義帖子類型中創建多個類別(分類法),然后在首頁查詢中基於特定的查詢效果很好。 目前,我有3種分類法:
我已經寫了可以拉這些代碼的代碼。 我遇到的問題是,在首頁上,僅當這些帖子也附加到“特色”分類法時,才需要拉這些帖子。 因此,為此的標准邏輯示例為:
如果分類法=當前特價且特色,則成功否則失敗
但是它的作用是將它們全部拉出,因為當前代碼是OR,而我需要AND
有什么想法嗎? (下面的代碼)
<?php
$post_type = 'coupons';
$tax = 'coupons_category';
$tax_terms = get_terms($tax);
if ($tax_terms):
foreach ($tax_terms as $tax_term):
echo '<div id="'.$tax_term->slug.'" class="coupon-box '.$tax_term->slug.'">';
$args = array(
'post_type' => $post_type,
"$tax" => array($tax_term->slug, 'featured'),
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1
);
$myQuery = null;
$myQuery = new WP_Query($args);
if($myQuery->have_posts()):
while ($myQuery->have_posts()) : $myQuery->the_post();
$price = get_field('price_image', $myQuery->ID);
$print = get_field('print', $myQuery->ID);
$product = get_field('product_box_image', $myQuery->ID);
$title = get_the_title();
$content = get_the_content();
echo '<div class="fourty9 left box center">';
echo '<h1>'.$title.'</h1>';
echo '<p class="center"><img src="'.$price.'" /></p>';
echo '<p>'.$content.'</p>';
echo '<p class="center"><a href="'.$print.'">Print Coupon</a></p>';
echo '<p class="center"><img src="'.$product.'" alt="Filter"></p>';
echo '</div>';
endwhile;
endif;
echo '</div>';
wp_reset_query();
endforeach;
endif;
?>
您可以嘗試使用此方法( tax
-使用分類標准。從3.1版開始不推薦使用,以“ tax_query”為准)
$args = array(
'post_type' => 'coupons',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'coupons_category',
'field' => 'slug',
'terms' => array( 'current-specials', 'featured' ),
'operator' => 'AND'
)
)
);
$query = new WP_Query( $args );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.