wp_get_split_terms()WP 4.2.0

Gets data about taxonomy terms that had the same ID and were split since WP version 4.2.

In earlier versions (below 4.2), taxonomy terms in the wp_terms table were recorded in such a way that if 2 terms from different taxonomies had the same names, they were simply attached to one post, i.e., both terms had one ID.

Since WP version 4.2, such terms are separated and each taxonomy term will have its unique ID, even if 2 terms from different taxonomies have the same names and slugs.

This function is for cases when you need to get data about such separation, after the separation has occurred.

How to update your database after the split to fit the new format, read in this note.

No Hooks.

Returns

Array. An array of new IDs, where the key is the taxonomy name.

Usage

wp_get_split_terms( $old_term_id );
$old_term_id(integer) (required)
ID of the previous taxonomy term that was split.

Examples

0

#1 Fix taxonomy item IDs in the 'featured_tags' option after splitting

The recommended fixing method is the split_shared_term hook. However, when the plugin is updated after splitting, it is no longer possible to stick to this hook. In this case wp_get_split_terms(), which stores split information, will help.

Suppose the plugin stored term IDs in the featured_tags option. We need to make sure that none of the terms stored in featured_tags have been split, and if so, we need to update the IDs in that option.

function featured_tags_check_for_split_terms() {

	$featured_tag_ids = get_option( 'featured_tags', array() );

	// Check if there are ID terms from the taxonomy `post_tag` that have been separated
	foreach ( $featured_tag_ids as $index => $featured_tag_id ) {

		$split_terms = wp_get_split_terms( $featured_tag_id, 'post_tag' );

		if ( ! empty( $split_terms ) ) {
			foreach ( $split_terms as $split_term ) {
				// Replacing old IDs with new ones
				$featured_tag_ids[ $index ] = $split_term['post_tag'];
			}
		}
	}

	// Update
	update_option( 'featured_tags', $featured_tag_ids );
}
0

#2 Let's check if there is data about taxonomy elements separation on the site

To check if the taxonomy elements have been separated at all, run this code:

$split_terms = get_option( '_split_terms', [] );

if( $split_terms ){
	// разделение было и есть данные о разделении
}
else {
	// данных о разделении нет (но код разделения скорее всего сработал)
	// просто у вас не оказалось терминов с одинаковыми ИД
}
print_r( $split_terms );

/* displays:
Array
(
	[3] => Array
		(
			[post_tag] => 591
		)

	[18] => Array
		(
			[post_tag] => 592
		)
)
*/

If array() prints out the result, you did not have terms with the same ID after the split and the option can be removed without consequences:

delete_option( '_split_terms' );

Changelog

Since 4.2.0 Introduced.

wp_get_split_terms() code WP 7.0

function wp_get_split_terms( $old_term_id ) {
	$split_terms = get_option( '_split_terms', array() );

	$terms = array();
	if ( isset( $split_terms[ $old_term_id ] ) ) {
		$terms = $split_terms[ $old_term_id ];
	}

	return $terms;
}