How to count posts of a category and of a category limited by a tag

I have two urls, and I want to know how many posts will be displayed there, when I click them. The goal is to show the visitor in the navigation of those urls, how many postings are found there.

As far as I can analyze those urls I would say it’s about a category and then about a category and a tag. But pleas tell me if I am wrong.

Here are the two Urls, which actually end up in a blog-roll page (pseudo code):

http://site_url()/category/<category-name>/
and
http://site_url()/category/<category-name>/<tag-name>

while category is the string "category"

Is my question somehow clear?

Do I have to use count_posts() function?

Solutions Collecting From Web of "How to count posts of a category and of a category limited by a tag"

You could run a new instantiation of WP_Query.

$query = new WP_Query([
    'category_name' => '<category_slug>',
    'tag'           => '<tag_slug>',
    'fields'        => 'ids', // To minimize the query, since we just need a count
]);

More on WP_Query

Then all you need to do is reference (integer) $query->found_posts.

Hope that helps. I don’t think there is a better way unfortunately.