_split_shared_term( int|object $term_id, int|object $term_taxonomy_id, bool $record = true ): int|WP_Error
- Since
- 4.2.0, 4.3.0
- Source
wp-includes/taxonomy.php:4271
Compatibility
- WordPress
- since 4.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$term_idint|object- ID of the shared term, or the shared term object.
$term_taxonomy_idint|object- ID of the term_taxonomy item to receive a new term, or the term_taxonomy object (corresponding to a row from the term_taxonomy table).
$recordbooloptional- Whether to record data about the split term in the options table. The recording process has the potential to be resource-intensive, so during batch operations it can be beneficial to skip inline recording and do it just once, after the batch is processed. Only set this to
falseif you know what you are doing.
Default: true.Default:true
Return value
int|WP_Error- When the current term does not need to be split (or cannot be split on the current database schema),
$term_idis returned. When the term is successfully split, the new term_id is returned. A WP_Error is returned for miscellaneous errors.
Performance profile
How much work a call to _split_shared_term() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 23–195
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via ->get_var().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 232.
Third-party callbacks on 'split_shared_term' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
do_action()called directly - sqldatabase query
->get_var()called directly - cacheobject cache
wp_cache_delete_multiple()one call below _split_shared_term() - serializeserialisation
maybe_unserialize()one call below _split_shared_term()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 36 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _split_shared_term() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 23–31 | ->prepare(), ->get_var() |
$check_term_id !== false | 37–45 | ->prepare(), ->get_var(), ->prepare(), ->get_var() |
$check_term_id === false && !empty($shared_term) | 64–72 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), __(), name() |
$check_term_id === false && empty($shared_term) | 76–84 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), __(), name() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 137–147 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 141–150 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 141–151 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 145–154 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 149–159 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 149–159 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 152–164 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 153–162 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
24 further outcomes, up to 195 instructions
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 153–163 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 153–162 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 153–163 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 156–167 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 156–168 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 157–166 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 157–166 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && !empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 160–171 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 161–171 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 164–176 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 164–176 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 165–174 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 165–175 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 168–179 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 168–180 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 168–179 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && !empty($children_tt_ids) | 168–180 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 169–178 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), ->get_results(), update_option(), do_action() |
$check_term_id === false && !empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 172–183 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && !empty($term_taxonomy) && empty($children_tt_ids) | 172–183 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 176–188 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 180–191 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && !empty($children_tt_ids) | 180–192 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
$check_term_id === false && empty($shared_term) && empty($term_taxonomy) && empty($children_tt_ids) | 184–195 | ->prepare(), ->get_var(), ->prepare(), ->get_var(), ->prepare(), ->get_row(), ->insert(), term_taxonomy_id(), ->prepare(), ->get_row(), ->prepare(), ->get_col(), clean_term_cache(), clean_term_cache(), ->prepare(), ->get_col(), array_merge(), get_option(), update_option(), ->get_results(), update_option(), do_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 232 instructions, 23–195 executed per call, 15 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while _split_shared_term() runs, in this order:
- do_action( split_shared_term )actionline 4393 (+122 into the body)
Fires after a previously shared taxonomy term is split into two separate terms.
Uses · 7
- __()Retrieves the translation of $text.
- clean_term_cache()Removes all of the term IDs from the cache.
- clean_taxonomy_cache()Cleans the caches for a taxonomy.
- get_option()Retrieves an option value based on an option name.
- update_option()Updates the value of an option that was already added.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Error::__construct()Initializes the error.
Used by · 2
- _wp_batch_split_terms()Splits a batch of shared taxonomy terms.
- wp_update_term()Updates term based on arguments provided.
Source code
function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) { global $wpdb; if ( is_object( $term_id ) ) { $shared_term = $term_id; $term_id = (int) $shared_term->term_id; } if ( is_object( $term_taxonomy_id ) ) { $term_taxonomy = $term_taxonomy_id; $term_taxonomy_id = (int) $term_taxonomy->term_taxonomy_id; } // If there are no shared term_taxonomy rows, there's nothing to do here. $shared_tt_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) ); if ( ! $shared_tt_count ) { return $term_id; } /* * Verify that the term_taxonomy_id passed to the function is actually associated with the term_id. * If there's a mismatch, it may mean that the term is already split. Return the actual term_id from the db. */ $check_term_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); if ( $check_term_id !== $term_id ) { return $check_term_id; } // Pull up data about the currently shared slug, which we'll use to populate the new one. if ( empty( $shared_term ) ) { $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) ); } $new_term_data = array( 'name' => $shared_term->name, 'slug' => $shared_term->slug, 'term_group' => $shared_term->term_group, ); if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) { return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error ); } $new_term_id = (int) $wpdb->insert_id; // Update the existing term_taxonomy to point to the newly created term. $wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $new_term_id ), array( 'term_taxonomy_id' => $term_taxonomy_id ) ); // Reassign child terms to the new parent. if ( empty( $term_taxonomy ) ) { $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); } $children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = %s", $term_id, $term_taxonomy->taxonomy ) ); if ( ! empty( $children_tt_ids ) ) { foreach ( $children_tt_ids as $child_tt_id ) { $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $new_term_id ), array( 'term_taxonomy_id' => $child_tt_id ) ); clean_term_cache( (int) $child_tt_id, '', false ); } } else { // If the term has no children, we must force its taxonomy cache to be rebuilt separately. clean_term_cache( $new_term_id, $term_taxonomy->taxonomy, false ); } clean_term_cache( $term_id, $term_taxonomy->taxonomy, false ); /* * Taxonomy cache clearing is delayed to avoid race conditions that may occur when * regenerating the taxonomy's hierarchy tree. */ $taxonomies_to_clean = array( $term_taxonomy->taxonomy );Changelog
Introduced in 4.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$record parameter. Also, $term_id and $term_taxonomy_id can now accept objects.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.