Taxonomy count per Post type

Below is the code i use to output the post tag (taxonomy) count/number. I want to be able to split the count based on post type that the taxonomy features in (rather than the total number).

So i have the default “post” Post type, aswell as “blogs”, & “pics”. I want the taxonomy count to display something like: x posts | x blogs | x Pics

            <?php
            $tags = get_tags( array('name__like' => "a", 'order' => 'ASC') );
            foreach ( (array) $tags as $tag ) { ?>
                <li>                            
                    <a href="<?php echo get_tag_link( $tag->term_id ) ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">          
                        <span class="name"><?php echo $tag->name ?></span>
                        <span class="number"><?php echo $tag->count ?></span>
                    </a>
                </li>
            <?php } ?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I needed to get the number of post per type per term so i created this small function:

function get_term_post_count_by_type($term,$taxonomy,$type){
    $args = array( 
        'fields' =>'ids', //we don't really need all post data so just id wil do fine.
        'posts_per_page' => -1, //-1 to get all post
        'post_type' => $type, 
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $term
            )
        )
     );
    $ps = get_posts( $args );
    if (count($ps) > 0){return count($ps);}else{return 0;}
}

and once you have this function you can change your code a bit to this:

<?php
$ptypes = array('post','blog','pic'); //array with all of your post types
$tags = get_tags( array('name__like' => "a", 'order' => 'ASC') );
foreach ( (array) $tags as $tag ) { ?>
<li>                            
        <a href="<?php echo get_tag_link( $tag->term_id ) ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">          
            <span class="name"><?php echo $tag->name ?></span>
        <span class="number">
            <?php 
                $count = 1;
                foreach($ptypes as $t){
                    echo get_term_post_count_by_type($tag,'post_tag',$t) . " " . $t;
                    if (count($ptypes) != $count){
                        echo " | ";
                        $count = $count + 1;
                    }
                }
            ?>
            </span>
    </a>
    </li>
<?php } ?>

Method 2

I think it’s better to set posts_per_page to 1 so as not to load in all of your posts (but only one), if you have thousands of posts that’s not a very eficient way of doing this.

Setting it to 0 won’t work, because WP_Query will check if ( empty( $q['posts_per_page'] ) ). 0 is empty(), so WP_Query won’t use it.

WP_Query will do a count of the posts that would be found so that can be used instead of selecting all posts then loading them all into memory, making this function more efficient than the accepted one.

/**
 * Count number of any given post type in the term
 *
 * @param string $taxonomy
 * @param string $term
 * @param string $postType
 * @return int
 */
function count_posts_in_term($taxonomy, $term, $postType = 'post') {
    $query = new WP_Query([
        'posts_per_page' => 1,
        'post_type' => $postType,
        'tax_query' => [
            [
                'taxonomy' => $taxonomy,
                'terms' => $term,
                'field' => 'slug'
            ]
        ]
    ]);

    return $query->found_posts;
}

Method 3

I’m using this within a nested loop for each term:

$terms = get_the_terms( $post->ID , 'yourtaxonomynamehere' );
if($terms) {
    foreach( $terms as $term ) {
        echo $term->count;
    }
}
?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x