Updating term counts in WordPress / Woocommerce

 

Last week I had a short hickup of Woocommerce regarding the numbers count of a few custom taxonomies. Then I noticed there is actually no function in WordPress to easily recount terms in WordPress. In Woocommerce settings there is a ‘recount terms’ function, but it looks like it doens’t recount any custom taxonomies you have working with Woo, only it’s own terms. Other than that, there is no button of the sorts to recounts terms anywhere.

For the new : ‘Term counts’  is the number of posts each of the used terms has. For instance the category ‘My kittens’ has 20 posts. These items are not counted every time for performance reasons ( querying the database every time ) but stored in the database as a certain count. This count is updated whenever a post is being saved, but of coure it can also go wrong every now and then’

 Even going online to find a quick fix for this issue was not very succesful. There are either very big plugins which do a lot of everything in the realm of terms and categories. Or there were broken ones. Anyway my terms were not being recounted any time soon.

So that’s why I decided to wip this short, quick ( and probably bad ) code together to perform this task. Intended use is when you quickly need to recount your terms, not something you would want to use in any production site.

<?php 
/*
Plugin Name: Termupdater
Description: Very simple and not ready for production plugin to update term counts. Use at your own peril. </a>.
Version: 0
Author: B. Schuiling
Author URI: http://www.weblogmechanic.com
*/

class termUpdater
{
  private $taxonomies = array();
  
  public function __construct() 
  {

    add_action("admin_menu", array($this,'add_admin_menu')); 
 
  }
  
  public function add_admin_menu() 
  {
    add_management_page("Term Updater","Term Updater",'manage_categories','term-updater', array($this, 'page')); 
  }
  
  public function update_term($tax) 
  {
    echo "TAX : $tax <br />"; 
    
    $terms = get_terms($tax);
    echo " FOUND " . count($terms) . " TERMS <br />"; 
    $termtax = array();
    foreach($terms as $term)
    {
      $termtax[] = $term->term_taxonomy_id; 
    }
    
    $bool = wp_update_term_count($termtax, $tax);
    if ($bool) 
      echo "All terms counted <br/>"; 
      
  }
  
  public function page() 
  {
    $this->taxonomies = get_taxonomies(); 
    
    if (isset($_POST) && isset($_POST["tax"])) 
    {
      $tax = sanitize_text_field($_POST["tax"]); 
      $this->update_term($tax); 
    }
   
    ?>
      <h3>Term Updater</h3>
      <form method="post">
      <select name="tax">
    <?php
    foreach($this->taxonomies as $tax => $name)
    {
      echo "<option value='" . $tax  . "'>" . $name . "</option>"; 
    }
    ?>
      </select>
      <input type="submit" class="button primary" name="submit" value="Go"> 
      </form>
    <?php
  }
  
  

}

$t = new termUpdater(); 
 

?>

Tags: , ,


Bas Schuiling

Bas Schuiling

Expert on everything web development and WordPress. Is pressing his words when version 1 came out and has been liking it ever since. More than 10 years of work experience in web development and writer of this blog.