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(
            'taxonomy' => $taxonomy,
            'field' => 'id',
            'terms' => intval($term)
   $ps = get_posts( $args );

   if (count($ps) > 0){
       return count($ps);
       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.

function my_cpt_columns($columns){
$screen = get_current_screen();
$type = get_post_type_object( $screen->post_type );


$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.

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.

Leave a Reply

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