_wp_batch_split_terms()
- Since
- 4.3.0
- Source
wp-includes/taxonomy.php:4379
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.
Performance profile
How much work a call to _wp_batch_split_terms() 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
- 33–99
- Plugin surface
- None
- Called by
- 0
Reaches the database via ->query().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 159.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- optionoption read or write
get_option()called directly - sqldatabase query
->query()called directly - hookthird-party callbacks
apply_filters()one call below _wp_batch_split_terms() - cacheobject cache
wp_cache_get()one call below _wp_batch_split_terms() - serializeserialisation
maybe_unserialize()one call below _wp_batch_split_terms() - querycontent query
get_terms()one call below _wp_batch_split_terms()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_batch_split_terms() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 33 | time(), ->prepare(), ->query(), get_option(), time(), wp_schedule_single_event() |
$lock_result | 39 | time(), ->prepare(), ->query(), get_option(), time(), time(), wp_schedule_single_event() |
| always | 44 | time(), ->prepare(), ->query(), time(), update_option(), ->get_results(), update_option(), delete_option() |
!$lock_result | 55 | time(), ->prepare(), ->query(), get_option(), time(), time(), update_option(), ->get_results(), update_option(), delete_option() |
| always | 85–88 | time(), ->prepare(), ->query(), time(), update_option(), ->get_results(), time(), wp_schedule_single_event(), array_keys(), ->get_results(), get_option(), array_keys(), update_option(), delete_option() |
!$lock_result | 96–99 | time(), ->prepare(), ->query(), get_option(), time(), time(), update_option(), ->get_results(), time(), wp_schedule_single_event(), array_keys(), ->get_results(), get_option(), array_keys(), update_option(), delete_option() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 159 | 33–99 | 13 | |
| 8.5 | 159 | 33–99 | 13 | |
| 8.4 | 159 | 33–99 | 13 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 163 | 33–103 | 13 | |
| 8.2 | 163 | 33–103 | 13 | |
| 8.1 | 163 | 33–103 | 13 | |
| 7.4 | 163 | 33–103 | 13 |
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.
Uses · 6
- get_option()Retrieves an option value based on an option name.
- wp_schedule_single_event()Schedules an event to run only once.
- update_option()Updates the value of an option that was already added.
- delete_option()Removes an option by name. Prevents removal of protected WordPress options.
- _split_shared_term()Creates a new term for a term_taxonomy item that currently shares its term with another term_taxonomy.
- _get_term_hierarchy()Retrieves children of taxonomy as term IDs.
Source code
function _wp_batch_split_terms() { global $wpdb; $lock_name = 'term_split.lock'; // Try to lock. $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'off') /* LOCK */", $lock_name, time() ) ); if ( ! $lock_result ) { $lock_result = get_option( $lock_name ); // Bail if we were unable to create a lock, or if the existing lock is still valid. if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) { wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); return; } } // Update the lock, as by this point we've definitely got a lock, just need to fire the actions. update_option( $lock_name, time() ); // Get a list of shared terms (those with more than one associated row in term_taxonomy). $shared_terms = $wpdb->get_results( "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id GROUP BY t.term_id HAVING term_tt_count > 1 LIMIT 10" ); // No more terms, we're done here. if ( ! $shared_terms ) { update_option( 'finished_splitting_shared_terms', true ); delete_option( $lock_name ); return; } // Shared terms found? We'll need to run this script again. wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); // Rekey shared term array for faster lookups. $_shared_terms = array(); foreach ( $shared_terms as $shared_term ) { $term_id = (int) $shared_term->term_id; $_shared_terms[ $term_id ] = $shared_term; } $shared_terms = $_shared_terms; // Get term taxonomy data for all shared terms. $shared_term_ids = implode( ',', array_keys( $shared_terms ) ); $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" ); // Split term data recording is slow, so we do it just once, outside the loop. $split_term_data = get_option( '_split_terms', array() ); $skipped_first_term = array(); $taxonomies = array(); foreach ( $shared_tts as $shared_tt ) { $term_id = (int) $shared_tt->term_id; // Don't split the first tt belonging to a given term_id. if ( ! isset( $skipped_first_term[ $term_id ] ) ) { $skipped_first_term[ $term_id ] = 1; continue; } if ( ! isset( $split_term_data[ $term_id ] ) ) { $split_term_data[ $term_id ] = array(); } // Keep track of taxonomies whose hierarchies need flushing. if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) { $taxonomies[ $shared_tt->taxonomy ] = 1; } // Split the term. $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false ); } // Rebuild the cached hierarchy for each affected taxonomy. foreach ( array_keys( $taxonomies ) as $tax ) {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.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.