wp_update_term_count()
云策文档标注
概述
wp_update_term_count() 函数用于更新分类法中的术语数量。它支持延迟计数机制,并可调用自定义回调函数。
关键要点
- 函数用于更新术语数量,基于 term_taxonomy_id 和分类法上下文。
- 参数包括 $terms(必需,整数或数组)、$taxonomy(必需,字符串)和 $do_deferred(可选,布尔值,默认 false)。
- 返回布尔值:成功返回 true,无术语时返回 false。
- 支持延迟计数,通过 wp_defer_term_counting() 控制。
代码示例
function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) {
static $_deferred = array();
if ( $do_deferred ) {
foreach ( (array) array_keys( $_deferred ) as $tax ) {
wp_update_term_count_now( $_deferred[ $tax ], $tax );
unset( $_deferred[ $tax ] );
}
}
if ( empty( $terms ) ) {
return false;
}
if ( ! is_array( $terms ) ) {
$terms = array( $terms );
}
if ( wp_defer_term_counting() ) {
if ( ! isset( $_deferred[ $taxonomy ] ) ) {
$_deferred[ $taxonomy ] = array();
}
$_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) );
return true;
}
return wp_update_term_count_now( $terms, $taxonomy );
}注意事项
- 如果应用了分类法回调,将调用它来更新计数。
- 默认操作是计算与 term ID 相关的术语数量,然后更新数据库。
- 延迟计数时,术语会被累积到 $_deferred 数组中,直到显式刷新。
原文内容
Updates the amount of terms in taxonomy.
Description
If there is a taxonomy callback applied, then it will be called for updating the count.
The default action is to count what the amount of terms have the relationship of term ID. Once that is done, then update the database.
Parameters
$termsint|arrayrequired-
The term_taxonomy_id of the terms.
$taxonomystringrequired-
The context of the term.
$do_deferredbooloptional-
Whether to flush the deferred term counts too.
Default:
false
Source
function wp_update_term_count( $terms, $taxonomy, $do_deferred = false ) {
static $_deferred = array();
if ( $do_deferred ) {
foreach ( (array) array_keys( $_deferred ) as $tax ) {
wp_update_term_count_now( $_deferred[ $tax ], $tax );
unset( $_deferred[ $tax ] );
}
}
if ( empty( $terms ) ) {
return false;
}
if ( ! is_array( $terms ) ) {
$terms = array( $terms );
}
if ( wp_defer_term_counting() ) {
if ( ! isset( $_deferred[ $taxonomy ] ) ) {
$_deferred[ $taxonomy ] = array();
}
$_deferred[ $taxonomy ] = array_unique( array_merge( $_deferred[ $taxonomy ], $terms ) );
return true;
}
return wp_update_term_count_now( $terms, $taxonomy );
}
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |