[wp-trac] [WordPress Trac] #56992: The Loop displays incorrect data for queries started with `fields => 'id=>parent'`.

WordPress Trac noreply at wordpress.org
Tue Mar 11 01:56:40 UTC 2025


#56992: The Loop displays incorrect data for queries started with `fields =>
'id=>parent'`.
--------------------------------------+----------------------------
 Reporter:  peterwilsoncc             |       Owner:  peterwilsoncc
     Type:  defect (bug)              |      Status:  reopened
 Priority:  normal                    |   Milestone:  6.8
Component:  Query                     |     Version:  3.1
 Severity:  normal                    |  Resolution:
 Keywords:  2nd-opinion dev-feedback  |     Focuses:  performance
--------------------------------------+----------------------------
Changes (by peterwilsoncc):

 * keywords:  2nd-opinion has-patch has-unit-tests dev-feedback => 2nd-
     opinion dev-feedback


Comment:

 > I'm doing myself a lot of questions with this topic. And one of the
 biggest questions that comes to my mind is: Isn't a workaround, without
 touching the core code, to achieve the same results?

 You're right, there is a work-around without touching WP Core but that's
 not really satisfactory. Requiring developers to work around a bug is not
 a good approach, as many developers will not know to work around it.

 > Because the baseline is performant by default, but your "upgrade" hits
 significantly the performance. If someone is currently using the
 performant version (which is the mostly likely since the new version did
 not exist for ages), he will have his site affected by this patch.

 It looks like there can be some performance improvements in how the code
 determines whether posts need to have their caches primed. I'll work up a
 patch.

 In terms of testing, I think extending `WP_Query` with the old method for
 testing will provide data that's closer to the real world use (see below),
 so I will test with that and using [https://github.com/Yoast/wp-cli-faker
 Yoast's content faker].


 {{{#!php
 <?php
 class WP_Query_Old extends WP_Query {
         /**
          * Sets up the current post.
          *
          * Retrieves the next post, sets up the post, sets the 'in the
 loop'
          * property to true.
          *
          * @since 1.5.0
          *
          * @global WP_Post $post Global post object.
          */
         public function the_post() {
                 global $post;

                 if ( ! $this->in_the_loop ) {
                         // Only prime the post cache for queries limited
 to the ID field.
                         $post_ids = array_filter( $this->posts,
 'is_numeric' );
                         // Exclude any falsey values, such as 0.
                         $post_ids = array_filter( $post_ids );
                         if ( $post_ids ) {
                                 _prime_post_caches( $post_ids,
 $this->query_vars['update_post_term_cache'],
 $this->query_vars['update_post_meta_cache'] );
                         }
                         $post_objects = array_map( 'get_post',
 $this->posts );
                         update_post_author_caches( $post_objects );
                 }

                 $this->in_the_loop = true;
                 $this->before_loop = false;

                 if ( -1 == $this->current_post ) { // Loop has just
 started.
                         /**
                          * Fires once the loop is started.
                          *
                          * @since 2.0.0
                          *
                          * @param WP_Query $query The WP_Query instance
 (passed by reference).
                          */
                         do_action_ref_array( 'loop_start', array( &$this )
 );
                 }

                 $post = $this->next_post();
                 $this->setup_postdata( $post );
         }
 }
 }}}

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


More information about the wp-trac mailing list