Make WordPress Core


Ignore:
Timestamp:
10/12/2023 11:39:05 PM (19 months ago)
Author:
peterwilsoncc
Message:

Query: Cache post parent IDs in posts group.

Move the cache of post parent IDs from the dedicated group post_parents to posts. This maintains backward compatibility for clearing all post object related data by calling wp_cache_flush_group( 'posts' ).

Post parent IDs are now cached with with the prefix post_parent: in the posts group.

Follow up to [56763].

Props spacedmonkey, joemcgill, peterwilsoncc.
See #59188.

File:
1 edited

Legend:

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

    r56815 r56925  
    31933193                        _prime_post_parent_id_caches( $post_ids );
    31943194
     3195                        $post_parent_cache_keys = array();
     3196                        foreach ( $post_ids as $post_id ) {
     3197                            $post_parent_cache_keys[] = 'post_parent:' . (string) $post_id;
     3198                        }
     3199
    31953200                        /** @var int[] */
    3196                         $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
    3197 
    3198                         foreach ( $post_parents as $id => $post_parent ) {
     3201                        $post_parents = wp_cache_get_multiple( $post_parent_cache_keys, 'posts' );
     3202
     3203                        foreach ( $post_parents as $cache_key => $post_parent ) {
    31993204                            $obj              = new stdClass();
    3200                             $obj->ID          = (int) $id;
     3205                            $obj->ID          = (int) str_replace( 'post_parent:', '', $cache_key );
    32013206                            $obj->post_parent = (int) $post_parent;
    32023207
     
    32463251
    32473252            /** @var int[] */
    3248             $post_parents = array();
    3249             $post_ids     = array();
     3253            $post_parents       = array();
     3254            $post_ids           = array();
     3255            $post_parents_cache = array();
    32503256
    32513257            foreach ( $this->posts as $key => $post ) {
     
    32553261                $post_parents[ (int) $post->ID ] = (int) $post->post_parent;
    32563262                $post_ids[]                      = (int) $post->ID;
     3263
     3264                $post_parents_cache[ 'post_parent:' . (string) $post->ID ] = (int) $post->post_parent;
    32573265            }
    32583266            // Prime post parent caches, so that on second run, there is not another database query.
    3259             wp_cache_add_multiple( $post_parents, 'post_parent' );
     3267            wp_cache_add_multiple( $post_parents_cache, 'posts' );
    32603268
    32613269            if ( $q['cache_results'] && $id_query_is_cacheable ) {
Note: See TracChangeset for help on using the changeset viewer.