_update_term_count_on_transition_post_status

Update the custom taxonomies' term counts when a post's status is changed.

Description

_update_term_count_on_transition_post_status( (string) $new_status, (string) $old_status, (WP_Post) $post ); 

For example, default posts term counts (for custom taxonomies) don't include private / draft posts.

Parameters (3)

0. $new_status (string)
New post status.
1. $old_status (string)
Old post status.
2. $post (WP_Post)
Post object.

Usage

  1. if ( !function_exists( '_update_term_count_on_transition_post_status' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // New post status. 
  5. $new_status = ''; 
  6.  
  7. // Old post status. 
  8. $old_status = ''; 
  9.  
  10. // Post object. 
  11. $post = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = _update_term_count_on_transition_post_status($new_status, $old_status, $post); 
  15.  

Defined (1)

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

/wp-includes/post.php  
  1. function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { 
  2. // Update counts for the post's terms. 
  3. foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { 
  4. $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); 
  5. wp_update_term_count( $tt_ids, $taxonomy );