Out of the box this cannot be done, as the post counts on each term is singular, there is no breakdown by post type.
In order to do this, you will need to output the markup yourself, and query for the counts yourself.
This will be slow/heavy/expensive.
So first we start with the current term/taxonomy:
$category_object = get_queried_object();
$current_category_taxonomy = $category_object->taxonomy;
$current_category_term_id = $category_object->term_id;
$current_category_name = $category_object->name;
Then we grab all child terms that aren’t empty:
$children = get_terms(
[
'taxonomy' => $current_category_taxonomy,
'parent' => $category_object->term_id,
]
);
And loop over each term:
foreach ( $children as $child ) {
//
}
Now inside that loop we need to check how many posts are of your particular post type, lets say the changeme
post type:
$args = [
'post_type' => 'changeme',
$term->taxonomy => $child->term_id,
'posts_per_page' => 1,
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
echo '<li>' . esc_html( $term->name ) . ' ( '. intval( $q->found_posts ) . ' ) </li>';
}
Notice we used the found_posts
parameter. At this point, it’s just a matter of wrapping the whole thing in a <ul>
tag, and expanding the markup to match what you want. You have the term object inside the loop to print out URLs etc with.
Don’t forget, this is expensive/slow/heavy, you will want to cache this, perhaps using a transient, or if you have an object cache or Elastic search present.
Or you could just register another taxonomy specific for this post type, and avoid all of this.