Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 } ?>
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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 } ?>
share|improve this answer
    
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 –  Blackbird 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. –  Blackbird 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? –  Blackbird Nov 10 '11 at 23:20
show 2 more comments

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;
    }
}
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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