clean_term_cache() WordPress Function

The clean_term_cache function is a core function in WordPress. It is used to clean the term cache. The term cache is a cache of all terms in the WordPress database. The term cache is used to improve performance when using WordPress taxonomies.

clean_term_cache( int|int[] $ids, string $taxonomy = '', bool $clean_taxonomy = true ) #

Removes all of the term IDs from the cache.


Parameters

$ids

(int|int[])(Required)Single or array of term IDs.

$taxonomy

(string)(Optional) Taxonomy slug. Can be empty, in which case the taxonomies of the passed term IDs will be used.

Default value: ''

$clean_taxonomy

(bool)(Optional) Whether to clean taxonomy wide caches (true), or just individual term object caches (false).

Default value: true


Top ↑

Source

File: wp-includes/taxonomy.php

function clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true ) {
	global $wpdb, $_wp_suspend_cache_invalidation;

	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
		return;
	}

	if ( ! is_array( $ids ) ) {
		$ids = array( $ids );
	}

	$taxonomies = array();
	// If no taxonomy, assume tt_ids.
	if ( empty( $taxonomy ) ) {
		$tt_ids = array_map( 'intval', $ids );
		$tt_ids = implode( ', ', $tt_ids );
		$terms  = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" );
		$ids    = array();

		foreach ( (array) $terms as $term ) {
			$taxonomies[] = $term->taxonomy;
			$ids[]        = $term->term_id;
		}
		wp_cache_delete_multiple( $ids, 'terms' );
		$taxonomies = array_unique( $taxonomies );
	} else {
		wp_cache_delete_multiple( $ids, 'terms' );
		$taxonomies = array( $taxonomy );
	}

	foreach ( $taxonomies as $taxonomy ) {
		if ( $clean_taxonomy ) {
			clean_taxonomy_cache( $taxonomy );
		}

		/**
		 * Fires once after each taxonomy's term cache has been cleaned.
		 *
		 * @since 2.5.0
		 * @since 4.5.0 Added the `$clean_taxonomy` parameter.
		 *
		 * @param array  $ids            An array of term IDs.
		 * @param string $taxonomy       Taxonomy slug.
		 * @param bool   $clean_taxonomy Whether or not to clean taxonomy-wide caches
		 */
		do_action( 'clean_term_cache', $ids, $taxonomy, $clean_taxonomy );
	}

	wp_cache_set( 'last_changed', microtime(), 'terms' );
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More