最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Count how many posts have a specified tag AND category

programmeradmin0浏览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..

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..

Share Improve this question edited May 27, 2020 at 6:19 JoBe asked May 26, 2020 at 18:09 JoBeJoBe 1712 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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();

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);
发布评论

评论列表(0)

  1. 暂无评论