_update_post_term_count

Will update term count based on object types of the current taxonomy.

Description

_update_post_term_count( (array) $terms, (object) $taxonomy ); 

Private function for the default callback for post_tag and category taxonomies.

Parameters (2)

0. $terms (array)
List of Term taxonomy IDs.
1. $taxonomy (object)
Current taxonomy object of terms.

Usage

  1. if ( !function_exists( '_update_post_term_count' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // List of Term taxonomy IDs. 
  5. $terms = array(); 
  6.  
  7. // Current taxonomy object of terms. 
  8. $taxonomy = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _update_post_term_count($terms, $taxonomy); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function _update_post_term_count( $terms, $taxonomy ) { 
  2. global $wpdb; 
  3.  
  4. $object_types = (array) $taxonomy->object_type; 
  5.  
  6. foreach ( $object_types as &$object_type ) 
  7. list( $object_type ) = explode( ':', $object_type ); 
  8.  
  9. $object_types = array_unique( $object_types ); 
  10.  
  11. if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) { 
  12. unset( $object_types[ $check_attachments ] ); 
  13. $check_attachments = true; 
  14.  
  15. if ( $object_types ) 
  16. $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); 
  17.  
  18. foreach ( (array) $terms as $term ) { 
  19. $count = 0; 
  20.  
  21. // Attachments can be 'inherit' status, we need to base count off the parent's status if so. 
  22. if ( $check_attachments ) 
  23. $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); 
  24.  
  25. if ( $object_types ) 
  26. $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); 
  27.  
  28. /** This action is documented in wp-includes/taxonomy.php */ 
  29. do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); 
  30. $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 
  31.  
  32. /** This action is documented in wp-includes/taxonomy.php */ 
  33. do_action( 'edited_term_taxonomy', $term, $taxonomy->name );