3

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 ) ?>">          
                        <span class="name"><?php echo $tag->name ?></span>
                        <span class="number"><?php echo $tag->count ?></span>
                    </a>
                </li>
            <?php } ?>
3

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 ) ?>">          
            <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 } ?>
  • Hi, thx for the answer. Unfortunately it doesn't seem to work. It just outputs "0" for each type (even post which worked before). I assume you put the function into your functions.php file right? Any ideas why this isn't working? Thx – Carpy Nov 10 '11 at 22:24
  • i changed the lazy coding at the end of the function, try that and make sure the array with the post types has the exact names your post types have. – Bainternet Nov 10 '11 at 22:27
  • Thxs again but still no luck :/. The post types are definitely right as i use them in queries around the site. – Carpy Nov 10 '11 at 22:51
  • just found the problem, bad copy paste, try the updated function. – Bainternet Nov 10 '11 at 23:11
  • Excellent mate, works gd thx. Minute issue, any way to get rid of the last | separator? – Carpy Nov 10 '11 at 23:20
1

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;
    }
}
?>
1

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

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.