函数文档

wp_update_term_count_now()

💡 云策文档标注

概述

wp_update_term_count_now() 函数用于立即更新指定分类法中的术语计数。它接受术语ID数组和分类法名称作为参数,并返回 true 表示操作完成。

关键要点

  • 函数参数:$terms(必需,术语ID数组)和 $taxonomy(必需,分类法名称)。
  • 返回值:始终返回 true。
  • 内部逻辑:优先使用分类法的 update_count_callback 回调函数,否则根据对象类型调用 _update_post_term_count() 或 _update_generic_term_count()。
  • 缓存清理:操作后调用 clean_term_cache() 清理术语缓存。
  • 相关函数:包括 _update_post_term_count()、_update_generic_term_count()、clean_term_cache() 和 get_taxonomy()。

代码示例

function wp_update_term_count_now( $terms, $taxonomy ) {
    $terms = array_map( 'intval', $terms );

    $taxonomy = get_taxonomy( $taxonomy );
    if ( ! empty( $taxonomy->update_count_callback ) ) {
        call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy );
    } else {
        $object_types = (array) $taxonomy->object_type;
        foreach ( $object_types as &$object_type ) {
            if ( str_starts_with( $object_type, 'attachment:' ) ) {
                list( $object_type ) = explode( ':', $object_type );
            }
        }

        if ( array_filter( $object_types, 'post_type_exists' ) == $object_types ) {
            // Only post types are attached to this taxonomy.
            _update_post_term_count( $terms, $taxonomy );
        } else {
            // Default count updater.
            _update_generic_term_count( $terms, $taxonomy );
        }
    }

    clean_term_cache( $terms, '', false );

    return true;
}

注意事项

  • 此函数自 WordPress 2.5.0 版本引入。
  • 主要用于内部调用,如 wp_update_term_count() 函数。
  • 确保传入的 $terms 为有效的术语ID数组,以避免错误。

📄 原文内容

Performs term count update immediately.

Parameters

$termsarrayrequired
The term_taxonomy_id of terms to update.
$taxonomystringrequired
The context of the term.

Return

true Always true when complete.

Source

function wp_update_term_count_now( $terms, $taxonomy ) {
	$terms = array_map( 'intval', $terms );

	$taxonomy = get_taxonomy( $taxonomy );
	if ( ! empty( $taxonomy->update_count_callback ) ) {
		call_user_func( $taxonomy->update_count_callback, $terms, $taxonomy );
	} else {
		$object_types = (array) $taxonomy->object_type;
		foreach ( $object_types as &$object_type ) {
			if ( str_starts_with( $object_type, 'attachment:' ) ) {
				list( $object_type ) = explode( ':', $object_type );
			}
		}

		if ( array_filter( $object_types, 'post_type_exists' ) == $object_types ) {
			// Only post types are attached to this taxonomy.
			_update_post_term_count( $terms, $taxonomy );
		} else {
			// Default count updater.
			_update_generic_term_count( $terms, $taxonomy );
		}
	}

	clean_term_cache( $terms, '', false );

	return true;
}

Changelog

Version Description
2.5.0 Introduced.