Title: _update_post_term_count
Published: April 25, 2014
Last modified: February 24, 2026

---

# _update_post_term_count( int[] $terms, WP_Taxonomy $taxonomy )

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Updates term count based on object types of the current taxonomy.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#description)󠁿

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#parameters)󠁿

 `$terms`int[]required

List of term taxonomy IDs.

`$taxonomy`[WP_Taxonomy](https://developer.wordpress.org/reference/classes/wp_taxonomy/)
required

Current taxonomy object of terms.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#source)󠁿

    ```php
    function _update_post_term_count( $terms, $taxonomy ) {
    	global $wpdb;

    	$object_types = (array) $taxonomy->object_type;

    	foreach ( $object_types as &$object_type ) {
    		list( $object_type ) = explode( ':', $object_type );
    	}

    	$object_types = array_unique( $object_types );

    	$check_attachments = array_search( 'attachment', $object_types, true );
    	if ( false !== $check_attachments ) {
    		unset( $object_types[ $check_attachments ] );
    		$check_attachments = true;
    	}

    	if ( $object_types ) {
    		$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
    	}

    	$post_statuses = array( 'publish' );

    	/**
    	 * Filters the post statuses for updating the term count.
    	 *
    	 * @since 5.7.0
    	 *
    	 * @param string[]    $post_statuses List of post statuses to include in the count. Default is 'publish'.
    	 * @param WP_Taxonomy $taxonomy      Current taxonomy object.
    	 */
    	$post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) );

    	foreach ( (array) $terms as $tt_id ) {
    		$count = 0;

    		// Attachments can be 'inherit' status, we need to base count off the parent's status if so.
    		if ( $check_attachments ) {
    			// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
    			$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 IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $tt_id ) );
    		}

    		if ( $object_types ) {
    			// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
    			$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 IN ('" . implode( "', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $tt_id ) );
    		}

    		/**
    		 * Fires when a term count is calculated, before it is updated in the database.
    		 *
    		 * @since 6.9.0
    		 *
    		 * @param int    $tt_id         Term taxonomy ID.
    		 * @param string $taxonomy_name Taxonomy slug.
    		 * @param int    $count         Term count.
    		 */
    		do_action( 'update_term_count', $tt_id, $taxonomy->name, $count );

    		/** This action is documented in wp-includes/taxonomy.php */
    		do_action( 'edit_term_taxonomy', $tt_id, $taxonomy->name );
    		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $tt_id ) );

    		/** This action is documented in wp-includes/taxonomy.php */
    		do_action( 'edited_term_taxonomy', $tt_id, $taxonomy->name );
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/taxonomy.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/taxonomy.php#L4147)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/taxonomy.php#L4147-L4212)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#hooks)󠁿

 [do_action( ‘edited_term_taxonomy’, int $tt_id, string $taxonomy, array $args )](https://developer.wordpress.org/reference/hooks/edited_term_taxonomy/)

Fires immediately after a term-taxonomy relationship is updated.

 [do_action( ‘edit_term_taxonomy’, int $tt_id, string $taxonomy, array $args )](https://developer.wordpress.org/reference/hooks/edit_term_taxonomy/)

Fires immediate before a term-taxonomy relationship is updated.

 [apply_filters( ‘update_post_term_count_statuses’, string[] $post_statuses, WP_Taxonomy $taxonomy )](https://developer.wordpress.org/reference/hooks/update_post_term_count_statuses/)

Filters the post statuses for updating the term count.

 [do_action( ‘update_term_count’, int $tt_id, string $taxonomy_name, int $count )](https://developer.wordpress.org/reference/hooks/update_term_count/)

Fires when a term count is calculated, before it is updated in the database.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#related)󠁿

| Uses | Description | 
| [esc_sql()](https://developer.wordpress.org/reference/functions/esc_sql/)`wp-includes/formatting.php` |

Escapes data for use in a MySQL query.

  | 
| [wpdb::update()](https://developer.wordpress.org/reference/classes/wpdb/update/)`wp-includes/class-wpdb.php` |

Updates a row in the table.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  | 
| [wpdb::get_var()](https://developer.wordpress.org/reference/classes/wpdb/get_var/)`wp-includes/class-wpdb.php` |

Retrieves one value from the database.

  | 
| [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)`wp-includes/class-wpdb.php` |

Prepares a SQL query for safe execution.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#)

| Used by | Description | 
| [wp_update_term_count_now()](https://developer.wordpress.org/reference/functions/wp_update_term_count_now/)`wp-includes/taxonomy.php` |

Performs term count update immediately.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_update_post_term_count/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_update_post_term_count%2F)
before being able to contribute a note or feedback.