Advertisement
Guest User

Limit shared tag count to custom post type

a guest
Jul 30th, 2012
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. <?php
  2. //function to put inside of functions.php
  3. function get_term_post_count_by_type($term,$taxonomy,$type){
  4.     $args = array(
  5.         'fields' =>'ids', //we don't really need all post data so just id wil do fine.
  6.         'posts_per_page' => -1, //-1 to get all post
  7.         'post_type' => $type,
  8.         'tax_query' => array(
  9.             array(
  10.                 'taxonomy' => $taxonomy,
  11.                 'field' => 'slug',
  12.                 'terms' => $term
  13.             )
  14.         )
  15.      );
  16.     $ps = get_posts( $args );
  17.     if (count($ps) > 0){return count($ps);}else{return 0;}
  18. }
  19.  
  20. //call it from the template like this, make sure to change the post type variable
  21. $ptypes = array('projects'); //array with all of your post types
  22. $tags = get_tags( array('order' => 'ASC') );
  23. foreach ( (array) $tags as $tag ) { ?>
  24. <li>                            
  25.         <a href="<?php echo get_tag_link( $tag->term_id ) ?>">          
  26.             <span class="name"><?php echo $tag->name ?></span>
  27.         <span class="number">
  28.             <?php
  29.                 $count = 1;
  30.                 foreach($ptypes as $t){
  31.                     echo get_term_post_count_by_type($tag,'post_tag',$t) . " " . $t;
  32.                     if (count($ptypes) != $count){
  33.                         echo " | ";
  34.                         $count = $count + 1;
  35.                     }
  36.                 }
  37.             ?>
  38.             </span>
  39.     </a>
  40.     </li>
  41. <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement