Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Patch for Fix Category Count 1.0 ( plugin for WordPress (the `inc-` files need to be under the `inc/` folder and have `inc-` removed from their filename). Fixes `<?` and `<?=` code compatibility via, fixes WordPress database …
=== Fix Category Count ===
Contributors: abda53, montanab
Donate link:
Tags: fix,category,count,repair
Requires at least: 3.0.1
Tested up to: 4.3.1
Stable tag: 4.3.1
License: GPLv2 or later
License URI:
This plugin will allow you to easily repair and fix your category count for posts, pages and custom post types.
== Description ==
This plugin will allow you to easily repair and fix your category count for posts, pages and custom post types
You may fix specific or all post type categories that you believe are incorrect.
It is always recommended that you have a backup of your database.
== Installation ==
1. To install this plugin, either upload the entire directory to your wp-content/plugins/ directory, upload the zip file in your admin plugins screen. You may also install the plugin through
2. Activate the plugin through the 'Plugins' menu in WordPress
3. The plugin may be found in the admin menu under Tools
== Upgrade Notice ==
There are no upgrades at this time
== Frequently Asked Questions ==
Why does the category count get messed up?
That is a great question.
== Screenshots ==
1. Wrong Category Count
2. Fixed Category Count
3. Admin view page
== Changelog ==
= 1.0 =
* Initial Development
== Possible Upcoming Features ==
* Show a report after completion of tables updated
== Please Read ==
If you like, or hate this plugin, please rate it on Wordpress. If you are having any issues with it, please leave a comment on the plugin page.
Plugin Name: Fix Category Count
Plugin URI:
Description: This plugin will allow you to easily repair and fix your category count for posts, pages and custom post types.
Version: 1.0
Author: Eric Lozaga
Author URI:
Copyright (c) 2015 a 53 minute production
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_once 'inc/fix_category_count_engine.php';
//admin menu
function fix_category_count_menu() {
add_submenu_page('tools.php', 'Fix Category Count', 'Fix Category Count', 'update_core', 'fix_category_count', 'fix_category_count_view');
add_action( 'admin_menu', 'fix_category_count_menu' );
function fix_category_count_view() {
require_once 'inc/fix_category_count_admin_view.php';
function fix_category_count_link($links) {
$tools_link = '<a href="'.admin_url('tools.php?page=fix_category_count', 'admin').'" style="color:red">Fix Now</a>';
array_unshift($links, $tools_link);
return $links;
add_filter("plugin_action_links_".plugin_basename(__FILE__), 'fix_category_count_link' );
function fix_category_nonce_message ($translation) {
if ($translation == 'Are you sure you want to do this?'){
return 'You do not have permission to do that.';
return $translation;
add_filter('gettext', 'fix_category_nonce_message');
if($_POST) {
if(!check_admin_referer('select_post_types_'.get_current_user_id() )){
echo 'That is not allowed'; exit;
$fixes = $_POST['fix'];
foreach($fixes AS $fix_id){
$fix = new Fix_Category_Count();
$fix->post_type = $fix_id;
$updated = TRUE;
<?php echo "<h2>" . __( 'Fix Category Count Settings', 'fix_category_count' ) . "</h2>"; ?>
<h3>Select Post Types to Fix</h3>
<form name="site_data_form" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<div class="sa_block">
$post_types = get_post_types(array('public'=>TRUE));
foreach($post_types AS $post_type){
<input type="checkbox" class="post_type" name="fix[]" id="fix_<?php echo $post_type; ?>" value="<?php echo $post_type; ?>" /> <label for="fix_<?php echo $post_type; ?>"><?php echo ucwords(preg_replace("/_/"," ",$post_type)); ?> (<?php echo $post_type;?>)</label><br />
<a href="#" class="select_boxes" rel="all" id="all">Select All</a> |
<a href="#" class="select_boxes" rel="none" id="none">Deselect All</a>
<?php wp_nonce_field('select_post_types_'.get_current_user_id()); ?>
<div class="submit">
<input type="submit" name="Submit" value="<?php _e('Fix Categories Now', '' ) ?>" />
<?php if($updated){ ?>
<div class="updated"><p><strong><?php _e('Categories Updated.' ); ?></strong></p></div>
<?php } ?>
<script type="text/javascript">
$('.post_type').each(function() {
this.checked = true;
$('.post_type').each(function() {
this.checked = false;
class Fix_Category_Count
public $post_type = NULL;
private $count = 0;
private $taxonomies;
public function post_type($post_type) {
$this->post_type = sanitize_text_field($post_type);
public function process(){
global $wpdb;
$this->taxonomies = $this->get_this_taxonomies();
return TRUE;
private function get_this_taxonomies(){
return get_object_taxonomies($this->post_type);
private function fix_categories_count(){
global $wpdb;
$t = $this->taxonomies;
foreach($this->taxonomies AS $tax){
$category_ids = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}term_taxonomy` WHERE taxonomy = '$tax'", ARRAY_A);
foreach($category_ids as $c) {
$cat_row = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}term_relationships` WHERE `term_taxonomy_id`='".$c['term_taxonomy_id']."'", ARRAY_A);
foreach($cat_row AS $row){
$wpdb->get_results("SELECT * FROM `{$wpdb->prefix}posts` where `post_status`='publish' and `post_type`='".$this->post_type."' and `ID`='".$row['object_id']."'", ARRAY_A);
if($wpdb->num_rows>0) {
$this->update_category_correct_count($c['term_taxonomy_id'], $count);
return TRUE;
private function update_category_correct_count($tax_id, $count){
global $wpdb;
$row = $wpdb->get_row("SELECT * FROM `{$wpdb->prefix}term_taxonomy` WHERE `term_taxonomy_id`='".$tax_id."'", ARRAY_A);
$wpdb->update($wpdb->prefix.'term_taxonomy', array('count'=>$count), array('term_id'=>$row['term_id']));
return TRUE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment