#63904 closed enhancement (fixed)
Add action hook when terms counts are updated
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.9 | Priority: | normal |
| Severity: | normal | Version: | 6.9 |
| Component: | Taxonomy | Keywords: | has-patch needs-testing has-unit-tests |
| Focuses: | performance | Cc: |
Description (last modified by )
With #42522 and #63562 merged and slated for 6.9, we have now achieved running the UPDATE queries for updating term counts only when we should, which is great.
What we still lack though is the presence of a dedicated action hook that runs only during those operations. The name of that hook could be something like update_term_count.
An example where this is needed is something we want to add in Yoast SEO where we need to update some database entries of our custom database tables and we need to do that only when a term count is updated. Considering that we're talking about an extra database query, we want to make sure that we only do that when a term count is actually updated and not with every update of terms/posts.
One could use the edit_term_taxonomy action for this, while checking for the existence of the $args parameter, but this feels hacky and having a more canonical and dedicated hook for this probably makes more sense.
Change History (11)
#1
@
3 months ago
- Description modified (diff)
- Milestone changed from Awaiting Review to 6.9
- Owner set to SergeyBiryukov
- Status changed from new to accepted
This ticket was mentioned in PR #9698 on WordPress/wordpress-develop by @LeonidasMilossis.
3 months ago
#3
- Keywords has-patch added; needs-patch removed
Trac ticket: https://core.trac.wordpress.org/ticket/63904
#4
follow-up:
↓ 5
@
3 months ago
I think you're right in that it should be at least that deep into the flow, but I went with adding it in an even deeper level, in _update_post_term_count() and in _update_generic_term_count(), because this ensures it will only run when it should, but also that it will definitely run then.
#5
in reply to:
↑ 4
@
3 months ago
- Keywords needs-testing added
Replying to LeonidasMilossis:
I think you're right in that it should be at least that deep into the flow, but I went with adding it in an even deeper level, in
_update_post_term_count()and in_update_generic_term_count(), because this ensures it will only run when it should, but also that it will definitely run then.
Got it - those internal helpers should be the better place for the action hook. The code looks good to me, adding needs-testing tag
#7
@
3 months ago
@mukesh27 I've applied the requested changes, not sure if I'm supposed to remove the relevant keyword myself.
This ticket was mentioned in Slack in #core by westonruter. View the logs.
7 weeks ago
#11
@
3 weeks ago
The miscellaneous developer-focused changes developer note mentioned that the new update_term_count hook was introduced in [60711]: https://make.wordpress.org/core/2025/11/17/miscellaneous-developer-focused-changes-in-6-9/.
IMO, I think the best place for a dedicated hook should be inside
wp_update_term_count_now()- https://core.trac.wordpress.org/browser/trunk/src/wp-includes/taxonomy.php#L3579, right after the taxonomy’supdate_count_callbackruns and.. just before the caches are cleared.Why? because this is where the hook should only fires when a real recount has actually happened, and not on every status change or autosave.
I might be wrong but any input is very much welcome...