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

I know how to count how many posts has a certain tag, or categoryFor example:$term_slug = 'some-post-tag';$te

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742349524a4427229.html

相关推荐

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

    I know how to count how many posts has a certain tag, or categoryFor example:$term_slug = 'some-post-tag';$te

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信