wppaste
WordPress

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

Since
2.3.0
Source
wp-includes/taxonomy.php:4193
Updates term count based on object types of the current taxonomy.

Description

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

Compatibility

WordPress
since 2.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$termsint[]
List of term taxonomy IDs.
$taxonomyWP_Taxonomy
Current taxonomy object of terms.

Performance profile

How much work a call to _update_post_term_count() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
35–47

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 151.

Plugin surface
4 hooks

Third-party callbacks on 'update_post_term_count_statuses', 'update_term_count', 'edit_term_taxonomy' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _update_post_term_count() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always35–39array_unique(), array_search(), apply_filters(), esc_sql()
always43–47array_unique(), array_search(), array_filter(), esc_sql(), apply_filters(), esc_sql()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15135–478
8.515135–478
8.415135–47812 fewer instructions than PHP 8.3
8.316335–478
8.216335–478
8.116335–478
7.416335–478

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while _update_post_term_count() runs, in this order:

  1. apply_filters( update_post_term_count_statuses )filterline 4224 (+31 into the body)

    Filters the post statuses for updating the term count.

  2. do_action( update_term_count )actionline 4249 (+56 into the body)

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

  3. do_action( edit_term_taxonomy )actionline 4252 (+59 into the body)

    Fires immediate before a term-taxonomy relationship is updated.

  4. do_action( edited_term_taxonomy )actionline 4256 (+63 into the body)

    Fires immediately after a term-taxonomy relationship is updated.

Uses · 3

  • esc_sql()Escapes data for use in a MySQL query.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • do_action()Calls the callback functions that have been added to an action hook.

Used by · 1

Source code

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, array() );		$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, array() );	}}

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.