Yes it is possible to count the posts with certain tags within a particular category. You can achieve this by using a combination of wp functions like WP_Query
to retrieve posts and get_terms
to retrieve the tags within the specified category.
<?php
$category_slug = 'donotallowpost';
$term_slugs = [ 'cat', 'bird', 'chinchilla', 'dog', 'ferret', 'hamster', 'horse', 'lizard', 'mouse', 'pig', 'poultry', 'rabbit', 'snake', 'turtle' ];
// Get the category ID using the slug
$category = get_category_by_slug($category_slug);
$category_id = $category->term_id;
// Get the tags within the specified category
$tags = get_terms( [
'taxonomy' => 'post_tag',
'hide_empty' => false, // Make sure to include tags with no posts
'object_ids' => get_objects_in_term( $category_id, 'category' ), // Get tags associated with the category
] );
// Initialize post count
$post_count = 0;
// Loop through each tag to count posts
foreach ($tags as $tag) {
$tag_post_count = new WP_Query( array(
'tag_id' => $tag->term_id,
'posts_per_page' => -1, // Retrieve all posts
'fields' => 'ids', // Only retrieve post IDs for performance
) );
$post_count += $tag_post_count->post_count;
}
echo $post_count;
?>