_update_term_count_on_transition_post_status( string $new_status, string $old_status, WP_Post $post )
- Since
- 3.3.0
- Source
wp-includes/post.php:8445
Description
For example, default posts term counts (for custom taxonomies) don't include private / draft posts.
Compatibility
- WordPress
- since 3.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
$new_statusstring- New post status.
$old_statusstring- Old post status.
$postWP_Post- Post object.
Performance profile
How much work a call to _update_term_count_on_transition_post_status() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 6–16
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via wp_get_object_terms().
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 52.
Third-party callbacks on 'update_post_term_count_statuses' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
wp_get_object_terms()called directly
Further down the call graph this can also reach cache, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
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_term_count_on_transition_post_status() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$new_status === false | 6 | none |
$new_status !== false | 15–16 | get_object_taxonomies() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 52 | 6–16 | 7 | |
| 8.5 | 52 | 6–16 | 7 | |
| 8.4 | 52 | 6–16 | 7 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 64 | 6–16 | 7 | |
| 8.2 | 64 | 6–16 | 7 | |
| 8.1 | 64 | 6–16 | 7 | |
| 7.4 | 64 | 6–16 | 7 |
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 · 1
One hook fires while _update_term_count_on_transition_post_status() runs, in this order:
- apply_filters( update_post_term_count_statuses )filterline 8453 (+8 into the body)
Filters the post statuses for updating the term count.
Uses · 4
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- wp_update_term_count()Updates the amount of terms in taxonomy.
Source code
function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { if ( $new_status === $old_status ) { return; } // Update counts for the post's terms. foreach ( (array) get_object_taxonomies( $post->post_type, 'objects' ) as $taxonomy ) { /** This filter is documented in wp-includes/taxonomy.php */ $counted_statuses = apply_filters( 'update_post_term_count_statuses', array( 'publish' ), $taxonomy ); /* * Do not recalculate term count if both the old and new status are not included in term counts. * This accounts for a transition such as draft -> pending. */ if ( ! in_array( $old_status, $counted_statuses, true ) && ! in_array( $new_status, $counted_statuses, true ) ) { continue; } /* * Do not recalculate term count if both the old and new status are included in term counts. * * This accounts for transitioning between statuses which are both included in term counts. This can only occur * if the `update_post_term_count_statuses` filter is in use to count more than just the 'publish' status. */ if ( in_array( $old_status, $counted_statuses, true ) && in_array( $new_status, $counted_statuses, true ) ) { continue; } $tt_ids = wp_get_object_terms( $post->ID, $taxonomy->name, array( 'fields' => 'tt_ids' ) ); wp_update_term_count( $tt_ids, $taxonomy->name ); }}Changelog
Introduced in 3.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 7.1.0 tag, from
src/wp-includes/post.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.