Make WordPress Core

Opened 3 months ago

Closed 3 months ago

Last modified 3 weeks ago

#63904 closed enhancement (fixed)

Add action hook when terms counts are updated

Reported by: leonidasmilossis's profile LeonidasMilossis Owned by: sergeybiryukov's profile SergeyBiryukov
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 SergeyBiryukov)

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 @SergeyBiryukov
3 months ago

  • Description modified (diff)
  • Milestone changed from Awaiting Review to 6.9
  • Owner set to SergeyBiryukov
  • Status changed from new to accepted

#2 @rollybueno
3 months ago

  • Keywords needs-patch added

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’s update_count_callback runs 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...

This ticket was mentioned in PR #9698 on WordPress/wordpress-develop by @LeonidasMilossis.


3 months ago
#3

  • Keywords has-patch added; needs-patch removed

#4 follow-up: @LeonidasMilossis
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 @rollybueno
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

#6 @mukesh27
3 months ago

  • Keywords changes-requested added

#7 @LeonidasMilossis
3 months ago

@mukesh27 I've applied the requested changes, not sure if I'm supposed to remove the relevant keyword myself.

#8 @LeonidasMilossis
3 months ago

  • Keywords has-unit-tests added; changes-requested removed

#9 @SergeyBiryukov
3 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 60711:

Taxonomy: Add update_term_count action that fires when term counts are updated.

This allows plugins to run custom queries only when a term count is actually updated and not on every update of terms or posts.

Follow-up to [60365], [60510].

Props leonidasmilossis, peterwilsoncc, mukesh27, rollybueno, SergeyBiryukov.
Fixes #63904.

This ticket was mentioned in Slack in #core by westonruter. View the logs.


7 weeks ago

#11 @desrosj
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/.

Note: See TracTickets for help on using tickets.