WP_Query::generate_cache_key
Generates 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 Query::generate cache key WP 6.8.2
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 clase. * * 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( $q['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 ); $last_changed = wp_cache_get_last_changed( 'posts' ); if ( ! empty( $this->tax_query->queries ) ) { $last_changed .= wp_cache_get_last_changed( 'terms' ); } $this->query_cache_key = "wp_query:$key:$last_changed"; return $this->query_cache_key; }