update_post_term_count_statuses
Allows you to change the list of post statuses included when updating taxonomy term counts.
This filter is applied when updating the number of posts associated with a term, such as a category or tag. By default, only posts with the publish status are counted.
The filter can include other statuses in the count, such as draft, pending, private, and custom statuses.
Important: After changing term post count logic through this filter, term counts must be updated manually.
For example, call wp_update_term_count_now() with the list of terms and the taxonomy.
Normally, the count is updated when a post associated with the term is updated.
Usage
add_filter( 'update_post_term_count_statuses', 'wp_kama_update_post_term_count_statuses_filter', 10, 2 );
/**
* Function for `update_post_term_count_statuses` filter-hook.
*
* @param string[] $post_statuses List of post statuses to include in the count.
* @param WP_Taxonomy $taxonomy Current taxonomy object.
*
* @return string[]
*/
function wp_kama_update_post_term_count_statuses_filter( $post_statuses, $taxonomy ){
// filter...
return $post_statuses;
}
- $post_statuses(string[])
- List of post statuses included in the count.
Default: ['publish'] - $taxonomy(WP_Taxonomy)
- Current taxonomy object whose term count is being updated.
Examples
#1 Include the custom archived status for the genre taxonomy
Include posts with the custom archived status when counting posts in terms of the genre taxonomy.
add_filter( 'update_post_term_count_statuses', 'my_post_term_count_add_archived_status', 10, 2 );
/**
* @param string[] $statuses List of post statuses to include in the count.
* @param WP_Taxonomy $taxonomy Current taxonomy object.
*
* @return string[]
*/
function my_post_term_count_add_archived_status( $statuses, $taxonomy ) {
if ( $taxonomy->name === 'genre' ) {
$statuses[] = 'archived';
}
return $statuses;
}
#2 Include every post status when counting posts in a term
Use the following code to include every possible post status when counting posts in a term:
add_filter( 'update_post_term_count_statuses', 'my_include_all_statuses', 10, 2 );
/**
* @param string[] $statuses List of post statuses to include in the count.
* @param WP_Taxonomy $taxonomy Current taxonomy object.
*
* @return string[]
*/
function my_include_all_statuses( $statuses, $taxonomy ) {
$all_statuses = get_post_stati( [], 'names' );
return $all_statuses;
}
#3 Include drafts and private posts when counting posts in categories
Include posts with the draft and private statuses when counting posts in categories:
add_filter( 'update_post_term_count_statuses', 'category_up_term_count_statuses', 10, 2 );
/**
* @param string[] $statuses List of post statuses to include in the count.
* @param WP_Taxonomy $taxonomy Current taxonomy object.
*
* @return string[]
*/
function category_up_term_count_statuses( $statuses, $taxonomy ) {
if ( $taxonomy->name === 'category' ) {
$statuses[] = 'draft';
$statuses[] = 'private';
}
return $statuses;
}Changelog
| Since 5.7.0 | Introduced. |
Where the hook is called
$post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) );
$counted_statuses = apply_filters( 'update_post_term_count_statuses', array( 'publish' ), $taxonomy );
