wp_update_term_count( int|array $terms, string $taxonomy, bool $do_deferred = false ): bool
- Since
- 2.3.0
- Source
wp-includes/taxonomy.php:3535
Description
If there is a taxonomy callback applied, then it will be called for updating the count.
The default action is to count what the amount of terms have the relationship of term ID. Once that is done, then update the database.
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|array- The term_taxonomy_id of the terms.
$taxonomystring- The context of the term.
$do_deferredbooloptional- Whether to flush the deferred term counts too. Default false.Default:
false
Return value
bool- If no terms will return false, and if successful will return true.
Performance profile
How much work a call to wp_update_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
- Scaling
- Scales with input
- Instructions
- 8–36
- Plugin surface
- None
- Called by
- 4
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 50.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_update_term_count() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($terms) | 8 | none |
empty($terms) | 14–15 | array_keys() |
!empty($terms) && !wp_defer_term_counting() | 17–19 | wp_defer_term_counting(), wp_update_term_count_now() |
!empty($terms) && !wp_defer_term_counting() | 23–26 | array_keys(), wp_defer_term_counting(), wp_update_term_count_now() |
!empty($terms) && wp_defer_term_counting() | 25–29 | wp_defer_term_counting(), array_merge(), array_unique() |
!empty($terms) && wp_defer_term_counting() | 31–36 | array_keys(), wp_defer_term_counting(), array_merge(), array_unique() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 50 instructions, 8–36 executed per call, 7 branches. The work does not change between versions.
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.
Uses · 2
- wp_update_term_count_now()Performs term count update immediately.
- wp_defer_term_counting()Enables or disables term counting.
Used by · 4
- _update_term_count_on_transition_post_status()Updates the custom taxonomies' term counts when a post's status is changed.
- wp_defer_term_counting()Enables or disables term counting.
- wp_remove_object_terms()Removes term(s) associated with a given object.
- wp_set_object_terms()Creates term and taxonomy relationships.
Source code
function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) { static $_deferred = array(); if ( $do_deferred ) { foreach ( (array) array_keys( $_deferred ) as $tax ) { wp_update_term_count_now( $_deferred[ $tax ], $tax ); unset( $_deferred[ $tax ] ); } } if ( empty( $terms ) ) { return false; } if ( ! is_array( $terms ) ) { $terms = array( $terms ); } if ( wp_defer_term_counting() ) { if ( ! isset( $_deferred[ $taxonomy ] ) ) { $_deferred[ $taxonomy ] = array(); } $_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) ); return true; } return wp_update_term_count_now( $terms, $taxonomy );}Changelog
Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.