Stackexchange中多次询问此主题。但大多数问题都没有答案或答案不完整。我的自定义帖子类型"Product" 和两种不同的分类法"Sectors" 和"Seasons". 现在我想展示产品count for Garden archive page(花园是部门分类学的术语),即have realtion with Winter(冬季是季节分类学的术语)。这三天我尽了最大努力,但我解决不了这个问题。请帮帮我。感谢您的关注。这也是一个很好的来源,但可以让它发挥作用https://wordpress.stackexchange.com/a/327150/196375
$sectors = get_queried_object();
$season = get_terms( array(
\'taxonomy\' => \'seasons\',
\'hide_empty\' => true,
\'fields\' => \'58\', // winter term id
) );
foreach ($current_taxs as $current_tax) {
foreach ($seasons as $seasons) {
$args = array(
\'post_type\' => \'product\',
\'post_status\'=>\'publish\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => $current_tax,
\'field\' => \'slug\',
\'terms\' => array( $current_tax ),
),
array(
\'taxonomy\' => \'season\',
\'field\' => \'slug\',
\'terms\' => array( $season ),
),
),
);
$query = new WP_Query( $args );
echo $query->post_count;
}
}
SO网友:Teymur Abbasov
SOLUTION
<?php
$args = array(
\'post_type\' => \'product\',
\'post_status\'=>\'publish\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'sectors\',
\'field\' => \'slug\',
\'terms\' => \'garden\',
),
array(
\'taxonomy\' => \'season\',
\'field\' => \'slug\',
\'terms\' => \'winter\',
),
),
);
$query = new WP_Query( $args );
echo $query->post_count ;
?>