Query: Bypass caching for filtered SELECTs.

Bypass caching within `WP_Query` when the `SELECT` clause has been modified via a filter. This prevents both cache key collisions and the returning of incomplete or unexpected results when the `SELECT` clause has been modified by an extender.

Props pypwalters, claytoncollie, johnwatkins0, TimothyBlynJacobs, costdev, spacedmonkey, peterwilsoncc.
Fixes #57012.


Built from https://develop.svn.wordpress.org/trunk@54768


git-svn-id: http://core.svn.wordpress.org/trunk@54320 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2022-11-09 00:28:16 +00:00
parent 72cc029989
commit 4c90be7e91
2 changed files with 16 additions and 1 deletions

View File

@ -3103,8 +3103,23 @@ class WP_Query {
* cannot be cached. Note the space before `RAND` in the string
* search, that to ensure against a collision with another
* function.
*
* If `$fields` has been modified by the `posts_fields`,
* `posts_fields_request`, `post_clauses` or `posts_clauses_request`
* filters, then caching is disabled to prevent caching collisions.
*/
$id_query_is_cacheable = ! str_contains( strtoupper( $orderby ), ' RAND(' );
$cachable_field_values = array(
"{$wpdb->posts}.*",
"{$wpdb->posts}.ID, {$wpdb->posts}.post_parent",
"{$wpdb->posts}.ID",
);
if ( ! in_array( $fields, $cachable_field_values, true ) ) {
$id_query_is_cacheable = false;
}
if ( $q['cache_results'] && $id_query_is_cacheable ) {
$new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request );
$cache_key = $this->generate_cache_key( $q, $new_request );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-54766';
$wp_version = '6.2-alpha-54768';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.