Update count for all categories in WordPress

/**
 * Updates the amount of terms (count) in taxonomy (category).
 *
 * @see https://wpcodebook.com/wordpress-update-count-all-categories/
 * @see https://developer.wordpress.org/reference/functions/wp_update_term_count/
 *
 * @return bool
 */
function wpcodebook_update_categories_count() {
	$taxonomy = 'category';
	$terms = get_terms( array(
		'taxonomy'   => $taxonomy,
		'hide_empty' => false,
	) );
	$term_ids = (
		! empty( $terms ) && ! is_wp_error( $terms ) ?
		wp_list_pluck( $terms, 'term_id' ) :
		array()
	);
	return wp_update_term_count( $term_ids, $taxonomy );
}

Leave a Comment