函数文档

_update_term_count_on_transition_post_status()

💡 云策文档标注

概述

_update_term_count_on_transition_post_status() 是一个 WordPress 函数,用于在文章状态变更时更新自定义分类法的术语计数。它确保术语计数仅包含特定状态的文章,例如默认仅统计“publish”状态。

关键要点

  • 函数在文章状态从 $old_status 变为 $new_status 时触发,更新关联的自定义分类法术语计数。
  • 通过 get_object_taxonomies() 获取文章类型的所有分类法对象,并遍历处理。
  • 使用 update_post_term_count_statuses 过滤器(默认包含 'publish')来确定哪些状态应计入术语计数。
  • 逻辑优化:如果新旧状态都不在计数状态中(如 draft -> pending),或都在计数状态中(通过过滤器扩展),则跳过重新计算。
  • 核心操作:通过 wp_get_object_terms() 获取术语的 tt_ids,然后调用 wp_update_term_count() 更新计数。

代码示例

function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
    if ( $new_status === $old_status ) {
        return;
    }

    // Update counts for the post's terms.
    foreach ( (array) get_object_taxonomies( $post->post_type, 'objects' ) as $taxonomy ) {
        /** This filter is documented in wp-includes/taxonomy.php */
        $counted_statuses = apply_filters( 'update_post_term_count_statuses', array( 'publish' ), $taxonomy );

        /*
         * Do not recalculate term count if both the old and new status are not included in term counts.
         * This accounts for a transition such as draft -> pending.
         */
        if ( ! in_array( $old_status, $counted_statuses, true ) && ! in_array( $new_status, $counted_statuses, true ) ) {
            continue;
        }

        /*
         * Do not recalculate term count if both the old and new status are included in term counts.
         *
         * This accounts for transitioning between statuses which are both included in term counts. This can only occur
         * if the `update_post_term_count_statuses` filter is in use to count more than just the 'publish' status.
         */
        if ( in_array( $old_status, $counted_statuses, true ) && in_array( $new_status, $counted_statuses, true ) ) {
            continue;
        }

        $tt_ids = wp_get_object_terms( $post->ID, $taxonomy->name, array( 'fields' => 'tt_ids' ) );
        wp_update_term_count( $tt_ids, $taxonomy->name );
    }
}

注意事项

  • 函数在 WordPress 3.3.0 版本引入,用于处理自定义分类法术语计数的动态更新。
  • 默认情况下,术语计数仅包括“publish”状态的文章,但可通过 update_post_term_count_statuses 过滤器扩展。
  • 相关函数包括 wp_update_term_count()、wp_get_object_terms()、get_object_taxonomies() 和 apply_filters(),用于实现计数逻辑。

📄 原文内容

Updates the custom taxonomies’ term counts when a post’s status is changed.

Description

For example, default posts term counts (for custom taxonomies) don’t include private / draft posts.

Parameters

$new_statusstringrequired
New post status.
$old_statusstringrequired
Old post status.
$postWP_Postrequired
Post object.

Source

function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
	if ( $new_status === $old_status ) {
		return;
	}

	// Update counts for the post's terms.
	foreach ( (array) get_object_taxonomies( $post->post_type, 'objects' ) as $taxonomy ) {
		/** This filter is documented in wp-includes/taxonomy.php */
		$counted_statuses = apply_filters( 'update_post_term_count_statuses', array( 'publish' ), $taxonomy );

		/*
		 * Do not recalculate term count if both the old and new status are not included in term counts.
		 * This accounts for a transition such as draft -> pending.
		 */
		if ( ! in_array( $old_status, $counted_statuses, true ) && ! in_array( $new_status, $counted_statuses, true ) ) {
			continue;
		}

		/*
		 * Do not recalculate term count if both the old and new status are included in term counts.
		 *
		 * This accounts for transitioning between statuses which are both included in term counts. This can only occur
		 * if the `update_post_term_count_statuses` filter is in use to count more than just the 'publish' status.
		 */
		if ( in_array( $old_status, $counted_statuses, true ) && in_array( $new_status, $counted_statuses, true ) ) {
			continue;
		}

		$tt_ids = wp_get_object_terms( $post->ID, $taxonomy->name, array( 'fields' => 'tt_ids' ) );
		wp_update_term_count( $tt_ids, $taxonomy->name );
	}
}

Hooks

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

Filters the post statuses for updating the term count.

Changelog

Version Description
3.3.0 Introduced.