[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 Jun 22 12:58:57 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:  Awaiting Review
Component:  Query         |    Version:  6.2.2
 Severity:  normal        |   Keywords:
  Focuses:  performance   |
--------------------------+-----------------------------
 Seems that there is a bug in if-statement in class-wp-query.php
 `get_posts()` function: https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/class-wp-query.php#L3520-L3522

 Can't see any reason why this if-statement doesn't have also `&&
 $id_query_is_cacheable`-check like earlier `if ( $q['cache_results'] )`
 -checks have in the same function.

 This can cause weird problems especially, when you have activated some
 persistent object cache solution in your site and you have modified the
 `$fields` variable for example via `posts_fields_request` -hook.. I have
 tested only with wp-redis, but would imagine this is an issue also with
 other persistent object cache solutions and maybe even without one.

 ### How to reproduce the issue?
 1. Fresh WP 6.2.2 and twentytwentythree theme installed
 2. Install and configure wp-redis (1.4.2)
 3. Add following php-snippet (for example as mu-plugin)

 {{{#!php
 <?php
 $args = [
         'post_type'        => 'page',
         'post_status'      => 'publish',
         'posts_per_page'   => 1,
         'suppress_filters' => false,
 ];

 add_filter( 'posts_fields_request', 'foo_allowed_fields', 20, 2 );
 $pages = get_posts( $args );
 remove_filter( 'posts_fields_request', 'foo_allowed_fields', 20 );

 function foo_allowed_fields( $fields, $wp_query ) {
         global $wpdb;


         $allowed_fields = [
                 'ID',
                 'post_title',
                 'post_name',
                 'post_parent',
                 'post_type',
                 'guid',
                 'menu_order',
         ];

         $new_fields = '';
         foreach ( $allowed_fields as $i => $field ) {
                 $new_fields .= "{$wpdb->prefix}posts.{$field}";

                 if ( $i + 1 < count( $allowed_fields ) ) {
                         $new_fields .= ', ';
                 }
         }

         return $new_fields;
 }

 }}}
 4. Run `wp cache flush`
 5. Load the webpage couple of times. Some weird things happens. For
 example I see Comments form in frontpage even though I have deactivated
 comments feature in the site. In our other site permalink structure brake
 causing multiple 404 responses.

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


More information about the wp-trac mailing list