Make WordPress Core


Ignore:
Timestamp:
10/03/2023 02:59:22 PM (10 months ago)
Author:
spacedmonkey
Message:

Query: Improve caching behavior for WP_Query when retrieving id=>parent fields

In [53941], the addition of query caching to WP_Query brought about an unintended issue when querying for fields equal to id=>parent. Specifically, on websites with object caching enabled and a substantial number of pages, the second run of this query triggered the _prime_post_caches function for id=>parent. This led to the unnecessary priming of post, meta, and term caches, even when only id and parent information were requested.

This commit addresses this issue by introducing a new function, _prime_post_parents_caches, which primes a dedicated cache for post parents. This cache is primed during the initial query execution. Subsequently, the wp_cache_get_multiple function is employed to retrieve all post parent data in a single object cache request, optimizing performance.

Additionally, this commit extends the coverage of existing unit tests to ensure the reliability of the changes.

Props kevinfodness, joemcgill, peterwilsoncc, LinSoftware, thekt12, spacedmonkey.
Fixes #59188

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r56656 r56763  
    31803180
    31813181                if ( $cached_results ) {
    3182                     if ( 'ids' === $q['fields'] ) {
    3183                         /** @var int[] */
    3184                         $this->posts = array_map( 'intval', $cached_results['posts'] );
    3185                     } else {
    3186                         _prime_post_caches( $cached_results['posts'], $q['update_post_term_cache'], $q['update_post_meta_cache'] );
    3187                         /** @var WP_Post[] */
    3188                         $this->posts = array_map( 'get_post', $cached_results['posts'] );
    3189                     }
    3190 
    3191                     $this->post_count    = count( $this->posts );
     3182                    /** @var int[] */
     3183                    $post_ids = array_map( 'intval', $cached_results['posts'] );
     3184
     3185                    $this->post_count    = count( $post_ids );
    31923186                    $this->found_posts   = $cached_results['found_posts'];
    31933187                    $this->max_num_pages = $cached_results['max_num_pages'];
    31943188
    31953189                    if ( 'ids' === $q['fields'] ) {
     3190                        $this->posts = $post_ids;
     3191
    31963192                        return $this->posts;
    31973193                    } elseif ( 'id=>parent' === $q['fields'] ) {
     3194                        _prime_post_parents_caches( $post_ids );
     3195
    31983196                        /** @var int[] */
    3199                         $post_parents = array();
    3200 
    3201                         foreach ( $this->posts as $key => $post ) {
     3197                        $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
     3198
     3199                        foreach ( $post_parents as $id => $post_parent ) {
    32023200                            $obj              = new stdClass();
    3203                             $obj->ID          = (int) $post->ID;
    3204                             $obj->post_parent = (int) $post->post_parent;
    3205 
    3206                             $this->posts[ $key ] = $obj;
    3207 
    3208                             $post_parents[ $obj->ID ] = $obj->post_parent;
     3201                            $obj->ID          = (int) $id;
     3202                            $obj->post_parent = (int) $post_parent;
     3203
     3204                            $this->posts[] = $obj;
    32093205                        }
    32103206
    32113207                        return $post_parents;
     3208                    } else {
     3209                        _prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
     3210                        /** @var WP_Post[] */
     3211                        $this->posts = array_map( 'get_post', $post_ids );
    32123212                    }
    32133213                }
     
    32573257                $post_ids[]                      = (int) $post->ID;
    32583258            }
     3259            // Prime post parent caches, so that on second run, there is not another database query.
     3260            wp_cache_add_multiple( $post_parents, 'post_parent' );
    32593261
    32603262            if ( $q['cache_results'] && $id_query_is_cacheable ) {
Note: See TracChangeset for help on using the changeset viewer.