此信息是出于历史目的。如果您对2015年之前的术语工作不感兴趣,则可以跳过本节。

在WordPress 4.2之前

具有相同slug的不同分类法中的术语共享一个单词ID。例如,标签和一个带有slug“新闻”的类别具有相同的术语ID。

WordPress 4.2+

从4.2开始,当这些共享条款之一更新时,它将被拆分:更新的术语将分配一个新的术语ID。

这对您意味着什么?

在绝大多数情况下,此更新是无缝且平稳的。但是,某些插件和主题在选项,元元,用户元或其他地方存储术语ID可能受到影响。

处理分裂

WordPress 4.2包括两个不同的工具,可帮助插件和主题的作者进行过渡。

split_shared_term钩

分配一个新术语ID时,新的split_shared_term动作被解雇。

以下是插件和主题作者如何利用此钩子以确保更新存储的术语ID的一些示例。

存储在选项中的术语ID

假设您的插件存储一个名为的选项featured_tags其中包含一系列术语ID([4, 6, 10])作为主页特色帖子部分的查询参数。

在此示例中,您会挂上split_shared_term操作,检查更新的术语ID是否在数组中,并在必要时进行更新。

/**
 * Update featured_tags option when a shared term gets split.
 *
 * @param int    $term_id          ID of the formerly shared term.
 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
 * @param string $taxonomy         Taxonomy for the split term.
 */
function wporg_featured_tags_split( int $term_id, int $new_term_id, int $term_taxonomy_id, string $taxonomy ): void {
	// we only care about tags, so we'll first verify that the taxonomy is post_tag.
	if ( 'post_tag' === $taxonomy ) {

		// get the currently featured tags.
		$featured_tags = get_option( 'featured_tags' );

		// if the updated term is in the array, note the array key.
		$found_term = array_search( $term_id, $featured_tags, true );
		if ( false !== $found_term ) {

			// the updated term is a featured tag! replace it in the array, save the new array.
			$featured_tags[ $found_term ] = $new_term_id;
			update_option( 'featured_tags', $featured_tags );
		}
	}
}
add_action( 'split_shared_term', 'wporg_featured_tags_split', 10, 4 );

存储在元后的术语ID

假设您的插件将一个术语ID存储在Post Meta中的页面上,以便您可以显示某个页面的相关帖子。

在这种情况下,您需要使用get_posts()功能以获取您的页面meta_key并更新meta_value匹配拆分术语ID。

/**
 * Update related posts term ID for pages
 *
 * @param int    $term_id          ID of the formerly shared term.
 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
 * @param string $taxonomy         Taxonomy for the split term.
 */
function wporg_page_related_posts_split( int $term_id, int $new_term_id, int $term_taxonomy_id, string $taxonomy ): void {
	// find all the pages where meta_value matches the old term ID.
	$page_ids = get_posts(
		array(
			'post_type'  => 'page',
			'fields'     => 'ids',
			'meta_key'   => 'meta_key',
			'meta_value' => $term_id,
		)
	);

	// if such pages exist, update the term ID for each page.
	if ( $page_ids ) {
		foreach ( $page_ids as $id ) {
			update_post_meta( $id, 'meta_key', $new_term_id, $term_id );
		}
	}
}
add_action( 'split_shared_term', 'wporg_page_related_posts_split', 10, 4 );

wp_get_split_term函数


使用split_shared_term挂钩是处理术语ID更改的首选方法。

但是,在某些情况下,术语在没有您的插件的情况下分开了split_shared_term行动。

WordPress 4.2存储有关已分类的分类术语的信息,并提供了wp_get_split_term()实用程序功能可帮助开发人员检索此信息。

考虑上面的情况,您的插件将术语ID存储在一个命名选项中featured_tags。您可能需要构建一个验证这些标签ID的函数(也许是在插件更新上运行),以确保没有一个特征标签已被拆分:

/**
 * Retrieve information about split terms and udpates the featured_tags option with the new term IDs.
 *
 * @return void
 */
function wporg_featured_tags_check_split() {
	$featured_tag_ids = get_option( 'featured_tags', array() );

	// check to see whether any IDs correspond to post_tag terms that have been split.
	foreach ( $featured_tag_ids as $index => $featured_tag_id ) {
		$new_term_id = wp_get_split_term( $featured_tag_id, 'post_tag' );

		if ( $new_term_id ) {
			$featured_tag_ids[ $index ] = $new_term_id;
		}
	}

	// save
	update_option( 'featured_tags', $featured_tag_ids );
}

注意wp_get_split_term()采用两个参数,$old_term_id$taxonomy并返回一个整数。

如果您需要检索与旧术语ID相关的所有拆分条款的列表,无论分类法如何wp_get_split_terms()

By zhuon

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注