wppaste
WordPress

WP_Query::generate_cache_key( array $args, string $sql ): string

Since
6.1.0
Source
wp-includes/class-wp-query.php:5010
Generates cache key.

Compatibility

WordPress
since 6.1.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

$argsarray
Query arguments.
$sqlstring
SQL statement.

Return value

string
Cache key.

Performance profile

How much work a call to WP_Query::generate_cache_key() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–12

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 138.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::generate_cache_key() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–8none
is_string($value) && $value12->remove_placeholder_escape()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1386–1214
8.51386–1214
8.41386–12143 fewer instructions than PHP 8.3
8.31416–1514
8.21416–1514
8.11416–151415 more instructions than PHP 7.4
7.412661–9312

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 · 2

Used by · 1

Source code

	protected function generate_cache_key( array $args, $sql ) {		global $wpdb; 		unset(			$args['cache_results'],			$args['fields'],			$args['lazy_load_term_meta'],			$args['update_post_meta_cache'],			$args['update_post_term_cache'],			$args['update_menu_item_cache'],			$args['suppress_filters']		); 		if ( empty( $args['post_type'] ) ) {			if ( $this->is_attachment ) {				$args['post_type'] = 'attachment';			} elseif ( $this->is_page ) {				$args['post_type'] = 'page';			} else {				$args['post_type'] = 'post';			}		} elseif ( 'any' === $args['post_type'] ) {			$args['post_type'] = array_values( get_post_types( array( 'exclude_from_search' => false ) ) );		}		$args['post_type'] = (array) $args['post_type'];		// Sort post types to ensure same cache key generation.		sort( $args['post_type'] ); 		/*		 * Sort arrays that can be used for ordering prior to cache key generation.		 *		 * These arrays are sorted in the query generator for the purposes of the		 * WHERE clause but the arguments are not modified as they can be used for		 * the orderby clause.		 *		 * Their use in the orderby clause will generate a different SQL query so		 * they can be sorted for the cache key generation.		 */		$sortable_arrays_with_int_values = array(			'post__in',			'post_parent__in',		);		foreach ( $sortable_arrays_with_int_values as $key ) {			if ( isset( $args[ $key ] ) && is_array( $args[ $key ] ) ) {				$args[ $key ] = array_unique( array_map( 'absint', $args[ $key ] ) );				sort( $args[ $key ] );			}		} 		// Sort and unique the 'post_name__in' for cache key generation.		if ( isset( $args['post_name__in'] ) && is_array( $args['post_name__in'] ) ) {			$args['post_name__in'] = array_unique( $args['post_name__in'] );			sort( $args['post_name__in'] );		} 		if ( isset( $args['post_status'] ) ) {			$args['post_status'] = (array) $args['post_status'];			// Sort post status to ensure same cache key generation.			sort( $args['post_status'] );		} 		// Add a default orderby value of date to ensure same cache key generation.		if ( ! isset( $args['orderby'] ) ) {			$args['orderby'] = 'date';		} 		$placeholder = $wpdb->placeholder_escape();		array_walk_recursive(			$args,			/*			 * Replace wpdb placeholders with the string used in the database			 * query to avoid unreachable cache keys. This is necessary because			 * the placeholder is randomly generated in each request.			 *			 * $value is passed by reference to allow it to be modified.			 * array_walk_recursive() does not return an array.			 */			static function ( &$value ) use ( $wpdb, $placeholder ) {				if ( is_string( $value ) && str_contains( $value, $placeholder ) ) {					$value = $wpdb->remove_placeholder_escape( $value );

Changelog

Introduced in 6.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-query.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.