WP_Query::generate_cache_key()protectedWP 6.1.0

Generate cache key.

Метод класса: WP_Query{}

Хуков нет.

Возвращает

Строку. Cache key.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->generate_cache_key( $args, $sql );
$args(массив) (обязательный)
Query arguments.
$sql(строка) (обязательный)
SQL statement.

Заметки

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

Список изменений

С версии 6.1.0 Введена.

Код WP_Query::generate_cache_key() WP 6.1.1

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']
	);

	$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.
		 */
		function ( &$value ) use ( $wpdb, $placeholder ) {
			if ( is_string( $value ) && str_contains( $value, $placeholder ) ) {
				$value = $wpdb->remove_placeholder_escape( $value );
			}
		}
	);

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

	$last_changed = wp_cache_get_last_changed( 'posts' );
	if ( ! empty( $this->tax_query->queries ) ) {
		$last_changed .= wp_cache_get_last_changed( 'terms' );
	}

	return "wp_query:$key:$last_changed";
}