函数文档

_update_post_term_count()

💡 云策文档标注

概述

_update_post_term_count() 是 WordPress 的一个私有函数,用于更新基于当前分类法对象类型的术语计数。它作为 post_tag 和 category 分类法的默认回调函数,通过数据库查询计算并更新术语关联的帖子数量。

关键要点

  • 函数类型:私有函数,主要用于 post_tag 和 category 分类法的术语计数更新。
  • 参数:接受 $terms(术语分类法 ID 数组)和 $taxonomy(当前分类法对象 WP_Taxonomy)。
  • 核心逻辑:从分类法对象类型中提取 post_type,处理附件类型(attachment)的特殊状态,并基于 post_statuses 过滤进行计数。
  • Hook 使用:包含 apply_filters('update_post_term_count_statuses') 用于过滤帖子状态,以及 do_action('update_term_count') 等动作钩子在计数更新前后触发。
  • 数据库操作:使用 $wpdb 执行 SQL 查询来统计术语关联的帖子数量,并更新 term_taxonomy 表。

代码示例

function _update_post_term_count( $terms, $taxonomy ) {
    global $wpdb;

    $object_types = (array) $taxonomy->object_type;

    foreach ( $object_types as &$object_type ) {
        list( $object_type ) = explode( ':', $object_type );
    }

    $object_types = array_unique( $object_types );

    $check_attachments = array_search( 'attachment', $object_types, true );
    if ( false !== $check_attachments ) {
        unset( $object_types[ $check_attachments ] );
        $check_attachments = true;
    }

    if ( $object_types ) {
        $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
    }

    $post_statuses = array( 'publish' );
    $post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) );

    foreach ( (array) $terms as $tt_id ) {
        $count = 0;

        if ( $check_attachments ) {
            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $tt_id ) );
        }

        if ( $object_types ) {
            $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status IN ('" . implode( "', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $tt_id ) );
        }

        do_action( 'update_term_count', $tt_id, $taxonomy->name, $count );
        do_action( 'edit_term_taxonomy', $tt_id, $taxonomy->name );
        $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $tt_id ) );
        do_action( 'edited_term_taxonomy', $tt_id, $taxonomy->name );
    }
}

注意事项

  • 此函数是私有函数,不建议在插件或主题中直接调用,应通过 wp_update_term_count_now() 等公共函数间接使用。
  • 函数内部使用 SQL 查询,需注意性能影响,特别是在处理大量术语时。
  • 附件(attachment)的计数逻辑特殊,考虑了继承状态(inherit)和父帖子状态。
  • 通过 apply_filters('update_post_term_count_statuses') 可以自定义用于计数的帖子状态列表。

📄 原文内容

Updates term count based on object types of the current taxonomy.

Description

Private function for the default callback for post_tag and category taxonomies.

Parameters

$termsint[]required
List of term taxonomy IDs.
$taxonomyWP_Taxonomyrequired
Current taxonomy object of terms.

Source

function _update_post_term_count( $terms, $taxonomy ) {
	global $wpdb;

	$object_types = (array) $taxonomy->object_type;

	foreach ( $object_types as &$object_type ) {
		list( $object_type ) = explode( ':', $object_type );
	}

	$object_types = array_unique( $object_types );

	$check_attachments = array_search( 'attachment', $object_types, true );
	if ( false !== $check_attachments ) {
		unset( $object_types[ $check_attachments ] );
		$check_attachments = true;
	}

	if ( $object_types ) {
		$object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
	}

	$post_statuses = array( 'publish' );

	/**
	 * Filters the post statuses for updating the term count.
	 *
	 * @since 5.7.0
	 *
	 * @param string[]    $post_statuses List of post statuses to include in the count. Default is 'publish'.
	 * @param WP_Taxonomy $taxonomy      Current taxonomy object.
	 */
	$post_statuses = esc_sql( apply_filters( 'update_post_term_count_statuses', $post_statuses, $taxonomy ) );

	foreach ( (array) $terms as $tt_id ) {
		$count = 0;

		// Attachments can be 'inherit' status, we need to base count off the parent's status if so.
		if ( $check_attachments ) {
			// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
			$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status IN ('" . implode( "', '", $post_statuses ) . "') OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) IN ('" . implode( "', '", $post_statuses ) . "') ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $tt_id ) );
		}

		if ( $object_types ) {
			// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration
			$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status IN ('" . implode( "', '", $post_statuses ) . "') AND post_type IN ('" . implode( "', '", $object_types ) . "') AND term_taxonomy_id = %d", $tt_id ) );
		}

		/**
		 * Fires when a term count is calculated, before it is updated in the database.
		 *
		 * @since 6.9.0
		 *
		 * @param int    $tt_id         Term taxonomy ID.
		 * @param string $taxonomy_name Taxonomy slug.
		 * @param int    $count         Term count.
		 */
		do_action( 'update_term_count', $tt_id, $taxonomy->name, $count );

		/** This action is documented in wp-includes/taxonomy.php */
		do_action( 'edit_term_taxonomy', $tt_id, $taxonomy->name );
		$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $tt_id ) );

		/** This action is documented in wp-includes/taxonomy.php */
		do_action( 'edited_term_taxonomy', $tt_id, $taxonomy->name );
	}
}

Hooks

do_action( ‘edited_term_taxonomy’, int $tt_id, string $taxonomy, array $args )

Fires immediately after a term-taxonomy relationship is updated.

do_action( ‘edit_term_taxonomy’, int $tt_id, string $taxonomy, array $args )

Fires immediate before a term-taxonomy relationship is updated.

apply_filters( ‘update_post_term_count_statuses’, string[] $post_statuses, WP_Taxonomy $taxonomy )

Filters the post statuses for updating the term count.

do_action( ‘update_term_count’, int $tt_id, string $taxonomy_name, int $count )

Fires when a term count is calculated, before it is updated in the database.

Changelog

Version Description
2.3.0 Introduced.