wp_update_term_count()
Updates/recalculates the number of posts in a taxonomy element (term).
To update the number of posts, you can set your own function. The name of such a function is set in the taxonomy object in the field update_count_callback when registering the taxonomy using register_taxonomy() or it can be set a little later by modifying the taxonomy data and setting the function name in the specified field.
By default, the following functions are used for counting:
- _update_post_term_count( $terms, $taxonomy ) - for posts that have just been published and are being attached to the term.
- _update_generic_term_count( $terms, $taxonomy ) - for other objects (for example, user) that are attached to the term.
By default, the number of term relationships with the post is counted, and after counting, the data is updated in the count field of the wp_term_taxonomy table.
This function is directly related to the function wp_defer_term_counting().
No Hooks.
Returns
true|false.
false– when there are no specified terms.true– when the field value was successfully updated.
Usage
wp_update_term_count( $terms, $taxonomy, $do_deferred );
- $terms(number/array) (required)
A number or an array of numbers from the
term_taxonomy_idfield of thewp_term_relationshipstable.Since WP 4.4 it should match the term ID. For more details read here.
- $taxonomy(string) (required)
- The name of the taxonomy to which the numbers specified in $terms belong.
- $do_deferred(true/false)
- An internal parameter. Not used in normal operation. Needed for the function wp_defer_term_counting()
Examples
#1 Updating the number of posts in categories
Let's update the number of posts in categories 12 and 13, for example when a new post has been added. Such an update is done by function wp_insert_post() by calling wp_set_object_terms().
wp_update_term_count( [ 12, 13 ], 'category' );
#2 How to recalculate the number of posts in all taxonomy terms?
To update the post count for all terms in a taxonomy in WordPress, you can use the function wp_update_term_count(). This function recalculates the number of posts associated with each term in the specified taxonomy and updates the term counters for each term in the database.
Here’s an example of how to update the post count for all terms in the category taxonomy:
$taxonomy = 'category';
$terms = get_terms( [
'taxonomy' => $taxonomy,
'hide_empty' => false,
] );
foreach ( $terms as $term ) {
wp_update_term_count( $term->term_id, $taxonomy );
}
In this example, we first retrieve all terms for the specified taxonomy using the get_terms() function. Then we loop through all terms and update the post count in them.
Note that depending on the number of terms and posts on your site, this operation may take some time.
Option if your site was running on WP 4.4 or earlier
For older versions of WP, the values of the taxonomy fields term_id and term_taxonomy_id were different, and ideally, you should pass the value of term_taxonomy_id to this function. You can read more about this here.
Therefore, for older sites, a more stable code would look like this:
$taxonomy = 'category';
$terms = get_terms( [
'taxonomy' => $taxonomy,
'hide_empty' => false,
] );
$tt_ids = wp_list_pluck( $terms, 'term_taxonomy_id' );
if ( $tt_ids ) {
wp_update_term_count( $tt_ids, $taxonomy );
}
Changelog
| Since 2.3.0 | Introduced. |