
Limit shared tag count to custom post type
By: a guest on
Jul 30th, 2012 | syntax:
PHP | size: 1.26 KB | hits: 39 | expires: Never
<?php
//function to put inside of functions.php
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;}
}
//call it from the template like this, make sure to change the post type variable
$ptypes = array('projects'); //array with all of your post types
$tags = get_tags( array('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 } ?>