0

I know how to count how many posts has a certain tag, or category

For example:

$term_slug = 'some-post-tag';
$term = get_term_by('slug', $term_slug, $post_tag);
echo $term->count;

BUT! Is there anyway to count how many posts that have a tag AND a specified category?

I want to count how many posts that have the tag(slug) "cat" and the category slug "allowpost"

Is this even possible?

Edit: if possible, it would be good if this is manageable via some solution similarly my first script, because this is going to be used on search result pages, and different post pages, so adding something to the loop itself won't work..

0

You could use WP_Query and specifically a tax_query:

    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array( 'allow-list' ),
            ),
            array(
                'taxonomy' => 'post_tag',
                'field'    => 'slug',
                'terms'    => array( 'cats' ),
            ),
        ),
    );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
      $count = $the_query->found_posts;
      echo 'Post count: ' . $count;
     // echo '<ul>';

     // while ( $the_query->have_posts() ) {
      //  $the_query->the_post();
       // echo '<li>' . get_the_title() . '</li>';
     // }
    //  echo '</ul>';
   } else {
     echo 'No posts found';
   }
/* Restore original Post Data */
wp_reset_postdata();
| improve this answer | |
  • how do I echo it? – JoBe May 26 at 18:58
  • Because I'm not seeing anything (I might add that I'm pretty new at php and WordPress.. – JoBe May 26 at 19:03
  • I added a line to echo $count . I've also added a regular "loop" so you can see where the post content could be displayed. See developer.wordpress.org/reference/classes/wp_query for all the many options of what you can query and developer.wordpress.org/themes/basics/the-loop for what you can display within The Loop. – Michelle May 26 at 19:22
  • but this script it lists all the posts? – JoBe May 26 at 19:27
  • I was thinking about a php snippet that simply displays how many posts that has the correct attributes, not displaying or listing them.. – JoBe May 26 at 20:02
0

This solved my issue, originally created by Mr. Prashant Singh @ WordPress forum

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.