Count number of posts with a specified tag AND category

Please try the following code:

$args = array(
  'posts_per_page' => -1,
  'tax_query' => array(
    'relation' => 'AND', // only posts that have both taxonomies will return.
      'taxonomy' => 'post_tag',
      'field'    => 'slug',
      'terms'    => 'your-tag-slug', //cat
      'taxonomy' => 'category',
      'field'    => 'slug',
      'terms'    => 'your-category-slug', //allowpost
$posts = get_posts($args);
$count = count($posts);

Basically in the code, we are checking if the posts are in both the taxonomies then send them in the output and then counting the total of them.

I hope this will help you.

@prashantvatsh Thank you so much, it works perfect!

I would never have gotten that script together, thx again!

Source link