[wp-trac] [WordPress Trac] #58599: WP_Query->get_posts: cache updated also when query is set to be not cacheable

WordPress Trac noreply at wordpress.org
Thu Aug 3 13:11:43 UTC 2023


#58599: WP_Query->get_posts: cache updated also when query is set to be not
cacheable
--------------------------+-----------------------------
 Reporter:  saulirajala   |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Future Release
Component:  Query         |     Version:  6.2
 Severity:  normal        |  Resolution:
 Keywords:  has-patch     |     Focuses:  performance
--------------------------+-----------------------------

Comment (by spacedmonkey):

 So looking into this issue, I have some thoughts.

 This issue seems unrelated to the changes to the WP_Query that have
 happened in the last couple of releases. The problem I see is, if you
 filter the fields that are requested in the database call, these fields
 are then passed to the `get_post` and then cached in object caching. This
 means if you do not request the post content for example, the post object
 is created and saved into object cache without the post content. This is a
 problem, as the sites with and without preseiant object cache would have
 incorrect value saved in the object cache.


 Looking at the code, I think a change like this might fix the detailed
 issue, to ensure that these values are not stored in cache. Also want to
 ensure that meta and term are still primed and no new database queries are
 added.

 {{{#!php
    if ( $this->posts ) {
         $this->post_count = count( $this->posts );

         /** @var WP_Post[] */
         $this->posts = array_map( 'get_post', $this->posts );

         if ( $q['cache_results'] && $id_query_is_cacheable ) {
                 update_post_cache( $this->posts );
         }

         $post_ids = wp_list_pluck( $this->posts, 'ID' );

         if ( $q['update_post_meta_cache'] ) {
                 update_postmeta_cache( $post_ids );
         }

         if ( $q['update_post_term_cache'] ) {
                 $post_types = array_map( 'get_post_type', $this->posts );
                 $post_types = array_unique( $post_types );
                 update_object_term_cache( $post_ids, $post_types );
         }

 }}}

 This code snippet also fixes a long standing issue, IMO where passing
 `cache_results` is passed and term and meta caches were not primed.

 What do you think of this code snippet @peterwilsoncc @saulirajala ?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58599#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list