The problem is that categories don’t store any information about which post types it’s been used on, just the total count. So if you want to know if a category is being used for a particular post type, you need to query all the posts in that category, and check which post type they are. There isn’t a WordPress function that does this, so you’d need to make one. The most efficient way to do this would be a probably be a custom SQL query.
function wpse_330111_get_categories_for_post_type( $post_type ) {
global $wpdb;
/**
* The SQL query for getting categories based on which post type they've
* been used on.
*/
$query = $wpdb->prepare(
"SELECT DISTINCT
t.*, tt.*
FROM
$wpdb->terms t
INNER JOIN
$wpdb->term_taxonomy tt ON
tt.term_id = t.term_id
LEFT JOIN
$wpdb->term_relationships tr ON
tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN
$wpdb->posts p ON
p.ID = tr.object_id
WHERE
tt.taxonomy = 'category' AND
p.post_status="publish" AND
p.post_type = %s
",
$post_type
);
$categories = $wpdb->get_results( $query );
/**
* If there's results, make sure to return a proper WP_Term object for each
* category.
*/
if ( ! empty( $categories ) ) {
$categories = array_map( 'get_term', $categories );
}
return $categories;
}
That function will return any categories that are attached to a published post of a given post type:
$categories = wpse_330111_get_categories_for_post_type( 'team_member' );