WP_Term_Query::generate_cache_key()protectedWP 6.2.0

Generate cache key.

Method of the class: WP_Term_Query{}

No Hooks.

Return

String. Cache key.

Usage

// protected - for code of main (parent) or child class
$result = $this->generate_cache_key( $args, $sql );
$args(array) (required)
WP_Term_Query arguments.
$sql(string) (required)
SQL statement.

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 6.2.0 Introduced.

WP_Term_Query::generate_cache_key() code WP 6.5.4

protected function generate_cache_key( array $args, $sql ) {
	global $wpdb;
	// $args can be anything. Only use the args defined in defaults to compute the key.
	$cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );

	unset( $cache_args['cache_results'], $cache_args['update_term_meta_cache'] );

	if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) {
		$cache_args['fields'] = 'all';
	}
	$taxonomies = (array) $args['taxonomy'];

	// Replace wpdb placeholder in the SQL statement used by the cache key.
	$sql = $wpdb->remove_placeholder_escape( $sql );

	$key          = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $sql );
	$last_changed = wp_cache_get_last_changed( 'terms' );
	return "get_terms:$key:$last_changed";
}