├── js └── stc.js ├── readme.md └── stc.php /js/stc.js: -------------------------------------------------------------------------------- 1 | ;(function($){ 2 | for(i in stc.counter){ 3 | var tax = stc.counter[i]; 4 | $("tbody#the-list").find("td:contains(" + tax.name + ")") 5 | .parent().find("td.column-posts a").text(tax.counter); 6 | 7 | /*if(0!=tax.counter) { 8 | $("tbody#the-list").find("td:contains(" + tax.name + ")") 9 | .parent().find("td.column-posts a").text(tax.counter); 10 | }else{ 11 | $("tbody#the-list").find("td:contains(" + tax.name + ")").parent().remove(); 12 | }*/ 13 | } 14 | })(jQuery); 15 | 16 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Shared Taxonomy Counter Fix for WordPress 2 | 3 | This plugin fixes the taxonomy counter in WordPress admin panel if the taxonomy is shared across multiple post types. It's a known bug in WordPress. 4 | 5 | Conside the following situation 6 | 7 | 1. You are using **"category"** in **posts** as well as in a cpt **"cpt-x"** 8 | 2. You have two posts, and both of them are listed in two categories *"Food"* and *"Beverage"* 9 | 3. You have two **"cpt-x"** posts, both of them are categorized as *"Food"* 10 | 4. Now, when you come to the ***categories*** menu (in WP Admin Panel) you will see that *"Food"* and *"Beverage"* are shwing wrong counter as **"3"** and **"1"**, while they should be **"1"** and **"1"** for posts, and **"2"** and **"0"** for **"cpt-x"**. 11 | 12 | It's a bug in WordPress, has been mentioned in Trac Ticket [#19031](https://core.trac.wordpress.org/ticket/19031) and countless times in different support sites as well as in StackExchange forums. This plugin is a quick fix for this known issue. -------------------------------------------------------------------------------- /stc.php: -------------------------------------------------------------------------------- 1 | https://core.trac.wordpress.org/ticket/19031, and countless times in different support sites as well as in StackExchange forums. 6 | * Version: 1.0 7 | * Author: Hasin Hayder From ThemeBucket 8 | * Author URI: http://themebucket.net 9 | * License: GPL 10 | */ 11 | 12 | defined( 'ABSPATH' ) or die( "No Direct Access" ); 13 | 14 | if ( ! class_exists( "SharedTaxonomyCounterFix" ) ) { 15 | 16 | class SharedTaxonomyCounterFix { 17 | 18 | public function __construct() { 19 | add_action( 'admin_enqueue_scripts', array( $this, 'stc_load_scripts' ) ); 20 | } 21 | 22 | function stc_load_scripts( $hook ) { 23 | if ( "edit-tags.php" == $hook ) { 24 | $stc_tax_name = $_GET['taxonomy']; 25 | $stc_post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : "post"; 26 | 27 | $stc_counter = []; 28 | $stc_terms = $this->stc_get_terms( $stc_tax_name ); 29 | foreach ( $stc_terms as $stc_term ) { 30 | $stc_counter[] = array( 31 | "name" => $stc_term->name, 32 | "counter" => $this->stc_get_term_counter( $stc_term->slug, $stc_post_type ) 33 | ); 34 | } 35 | 36 | wp_enqueue_script( "jquery" ); 37 | wp_enqueue_script( "stc-js", plugin_dir_url( __FILE__ ) . "js/stc.js", "jquery", "1.0", true ); 38 | wp_localize_script( "stc-js", "stc", 39 | array( 40 | "counter" => $stc_counter 41 | ) 42 | ); 43 | } 44 | } 45 | 46 | function stc_get_term_counter( $term, $cpt ) { 47 | $stc_posts = new WP_Query( array( 48 | 'post_type' => $cpt, 49 | 'tax_query' => array( 50 | array( 51 | 'taxonomy' => 'category', 52 | 'field' => 'slug', 53 | 'terms' => $term, 54 | ) 55 | ), 56 | ) ); 57 | 58 | return $stc_posts->post_count; 59 | } 60 | 61 | function stc_get_terms( $tax = "category" ) { 62 | $stc_terms = get_categories( array( 63 | 'taxonomy' => $tax, 64 | 'hide_empty' => true 65 | 66 | ) ); 67 | 68 | return $stc_terms; 69 | } 70 | 71 | } 72 | 73 | $stc = new SharedTaxonomyCounterFix(); 74 | } 75 | 76 | --------------------------------------------------------------------------------