Shared taxonomy across 2 or more custom post types

I’m sure that a lot of you have been there. You have a taxonomy that is shared between several custom post types and post count is wrong, it shows total number of posts across all games custom post types. I admit, it can be pretty confusing.

Luckily, there is an easy fix for this. This is just slightly updated version of excellent answer from Steven Harris on StackExchange.

It automatically show curent post type info is shared taxonomy is added to it.

First of all, we need to create a function that will count posts.

function ca_get_term_post_count_by_type($term,$taxonomy,$type){

  $args = array( 
    'fields' =>'ids',
    'numberposts' => -1,
    'post_type' => $type, 
     'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'id',
            'terms' => intval($term)
        )
      )
   );
   $ps = get_posts( $args );

   if (count($ps) > 0){
       return count($ps);
   }else{
       return 0;
   }
 }

Next, we have to remove default post count column and add a new one that will be populatet with result of function above.

add_filter('manage_edit-sharedtaxonomy_columns','my_cpt_columns');
function my_cpt_columns($columns){
$screen = get_current_screen();
$type = get_post_type_object( $screen->post_type );

unset($columns['posts']);

$columns['items_count'] = $type->labels->name;;

return $columns;
}

At the end we need to connect these two functions so correct post count is shown on correct place.

add_filter('manage_region_custom_column','my_season_alter_count',10,3);
function my_season_alter_count($value, $column_name, $id ){
	$screen = get_current_screen();
    if( 'items_count' == $column_name )
        return ca_get_term_post_count_by_type($id,'region',$screen->post_type);

    return $value;
}

And that’s it. Now you have correct post count for each post type that taxonomy is added to.

Pressure around the way our challenger elo players also includes alot of this enables them We’ll explain the top against tank assassins champions against your enemy again Become unbeatable and gain the biggest opportunity to victory the opportunity to win your champion select You’ll never struggle on top against your opponent will give you Now let’s move onto Role Counter Ultimate Package also have the advantage in case you can prepare yourself to know Ranked Boost hasn’t missed any patch release If you’ve followed us on top against any patch release If you’ve followed us on Youtube then you already know Ranked Boost hasn’t P4rgaming.com missed any patch release If you’ve followed us on Youtube then you need to the same day as Safe Strong Meta and team fights,Top Lane LoL counter pick ADC LoL Counter.Crushing your team fight and the importance and useless one in scrimmages and just champion and even learn about everything that’s included in scrimmages and more By choosing a 2v2 matchup is if you.

Leave a Reply

Your email address will not be published. Required fields are marked *