检查了源代码,完成此操作的唯一方法似乎是过滤每个术语的输出计数,您可以通过在get\\u terms调用之前插入此过滤器来实现这一点。请注意,这将始终显示已发布的项目计数,因此请注意其用法。
function get_terms_filter_published( $terms, $taxonomies, $args ) {
global $wpdb;
$taxonomy = $taxonomies[0];
if ( ! is_array($terms) && count($terms) < 1 ) {
return $terms;
}
$filtered_terms = array();
$ctr = 0;
foreach ( $terms as $term ) {
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND p.post_status = \'publish\' LIMIT 1");
$published_terms[ $ctr ] = $term;
if ( intval($result) > 0 ) {
$published_terms[ $ctr ] = $term;
} else {
// you can comment this out if you don\'t want to show empty terms
$published_terms[ $ctr ]->count = 0;
}
$ctr++;
}
return $published_terms;
}
add_filter(\'get_terms\', \'get_terms_filter_published\', 10, 3);
$taxonomy = \'item_category\';
$args = array(
\'hide_empty\' => false,
\'orderby\' => \'name\',
\'order\' => \'ASC\'
);
$terms = get_terms( $taxonomy , $args );
foreach( $terms as $term ) {
echo $term->name . \' - \' . $term->count . \'<br/>\';
}