Title: WP_Query::generate_cache_key
Published: November 2, 2022
Last modified: May 20, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#wp--skip-link--target)

Generates cache key.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#parameters)󠁿

 `$args`arrayrequired

Query arguments.

`$sql`stringrequired

SQL statement.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#return)󠁿

 string Cache key.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#source)󠁿

    ```php
    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 );
    			}
    		}
    	);

    	ksort( $args );

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

    	$this->query_cache_key = "wp_query:$key";
    	return $this->query_cache_key;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/class-wp-query.php#L4975)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wp-query.php#L4975-L5067)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#related)󠁿

| Uses | Description | 
| [wpdb::remove_placeholder_escape()](https://developer.wordpress.org/reference/classes/wpdb/remove_placeholder_escape/)`wp-includes/class-wpdb.php` |

Removes the placeholder escape strings from a query.

  | 
| [wpdb::placeholder_escape()](https://developer.wordpress.org/reference/classes/wpdb/placeholder_escape/)`wp-includes/class-wpdb.php` |

Generates and returns a placeholder escape string for use in queries returned by ::prepare().

  | 
| [get_post_types()](https://developer.wordpress.org/reference/functions/get_post_types/)`wp-includes/post.php` |

Gets a list of all registered post type objects.

  |

[Show 1 more](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#)

| Used by | Description | 
| [WP_Query::get_posts()](https://developer.wordpress.org/reference/classes/wp_query/get_posts/)`wp-includes/class-wp-query.php` |

Retrieves an array of posts based on query variables.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_query/generate_cache_key/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.1.0](https://developer.wordpress.org/reference/since/6.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_query%2Fgenerate_cache_key%2F)
before being able to contribute a note or feedback.