WP 4.2之前
在WP 4.2之前,具有相同slug的不同分类法中的分类项(term)共享一个分类项(term) ID。
例如,一个标签(Tag)和一个类别(Category)具有相同的slug:“news”,则它们有相同的Term ID。
WP 4.2+
从WP 4.2开始,当其中一个共享term被更新时,它将被拆分:更新后的term将被分配一个新的term ID。
这对你意味着什么
在绝大多数情况下,此更新将是无缝且平静的。然而,一些在options、post-meta、user-meta或其他地方存储分类项(term)ID的插件和主题可能会受到影响。
处理拆分
WP 4.2包括两种不同的工具,帮助插件和主题的作者进行转换。
split_shared_term 钩子
当一个共享项被分配一个新的项ID时,一个新的split_shared_term
动作被触发。
以下是插件和主题作者如何利用这个钩子来确保更新存储的term ID的几个示例。
存储在选项中的term ID
假设您的插件存储了一个名为featured_tags
的选项,该选项包含一个分类项(term)ID数组([4, 6, 10]
),用作主页特色文章部分的查询参数。
在本例中,您将挂钩到split_shared_term
动作,检查更新的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($term_id, $new_term_id, $term_taxonomy_id, $taxonomy) { // we only care about tags, so we'll first verify that the taxonomy is post_tag. if ($taxonomy === 'post_tag') { // 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); if ($found_term !== false) { // 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);
存储在post meta中的term ID
假设你的插件在页面的post meta中存储了一个term ID,这样你就可以显示某个页面的相关文章。
在这种情况下,您需要使用get_posts()函数获取带有meta_key
的页面,并更新与分割term ID匹配的meta_value
。
/** * 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($term_id, $new_term_id, $term_taxonomy_id, $taxonomy) { // find all the pages where meta_value matches the old term ID. $page_ids = get_posts([ '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 函数
Note:使用split_shared_term
钩子是处理分类项(term)ID更改的首选方法。
然而,在某些情况下,分类项被拆分,而插件没有机会钩住split_shared_term
动作。
WP 4.2存储关于已拆分的分类项的信息,并提供wp_get_split_term()实用程序函数来帮助开发人员检索此信息。
考虑上述情况,插件将term ID存储在名为featured_tags
的选项中。
您可能希望构建一个验证这些标签ID的函数(可能在插件更新时运行),以确保没有拆分任何特征标签:
function wporg_featured_tags_check_split() { $featured_tag_ids = get_option('featured_tags', []); // 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
,并返回一个整数。
如果需要检索与旧分类项(term)ID相关联的所有拆分term的列表,而不考虑分类,请使用wp_get_split_terms()。