wp_get_split_terms()
Get data about terms that previously shared a single term_id, but have since been split.
No Hooks.
Return
Array
. Array of new term IDs, keyed by taxonomy.
Usage
wp_get_split_terms( $old_term_id );
- $old_term_id(int) (required)
- Term ID. This is the old, pre-split term ID.
Examples
#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 ); }
#2 Check if the taxonomy elements were splited on the site
To check if the taxonomy elements were separated at all, run this code by putting it into functions.php:
$split_terms = get_option( '_split_terms', array() ); wp_die( '<pre>'. print_r( $split_terms, 1 ) .'</pre>' ); /* will output: Array ( [3] => Array ( [post_tag] => 591 ) [18] => Array ( [post_tag] => 592 ) ) */
If the result prints array()
, there was no separation and the option can be removed without consequences:
delete_option( '_split_terms' );
Changelog
Since 4.2.0 | Introduced. |
wp_get_split_terms() wp get split terms code WP 6.1.1
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; }