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

WordPress Trac noreply at wordpress.org
Wed Feb 26 23:39:10 UTC 2025


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

Comment (by SirLouen):

 Replying to [comment:12 peterwilsoncc]:
 > I had the expectation and the actual reversed in the test. It should
 read `$this->assertEqualSetsWithIndex( $specific_post, $global_post );`
 >
 > Running the tests with the expected value first shows the issue
 correctly:
 >

 > The issue is that `WP_Query::the_post()` is used to populate the global
 post object while running the
 [https://developer.wordpress.org/reference/classes/wp_query/#standard-loop
 standard loop] on a secondary query.
 >
 >
 > In the current form, the code populates the global post incompletely so
 running code to get post data while in the loop will return empty values
 for `get_the_title()`, `get_the_content()` and `get_the_excerpt()`.
 >
 > For most other function calls to get the global post objects data, the
 value will be incorrect. `comments_open()` returns `false` instead of
 `true`, etc.

 I'm not sure if I'm not understanding you, or I'm not explaining well.

 Your test is made ad-hoc to proof some behavior that I'm not 100% sure if
 it's the expected behavior, maybe it is, but im not sure.

 More specifically here: https://github.com/WordPress/wordpress-
 develop/pull/7985/files#diff-
 1d050668621cbc3028e027d15e68438f4d879dd6e80ba98f9c9f69b9fb5469d0R3728

 You have to issue an extra query with get_post to fetch the full of it.

 But theoretically, with 'id=>parent' you are just saying that it returns
 an associative array of post IDs and their parent IDs not the whole data.
 In $global_post we should only find that, right?

 When you only need the ID, retrieving the full post object is unnecessary
 and consumes more resources and if you are working with large datasets, it
 also consumes more memory.

 Again, perhaps I'm wrong, but theoretically this is the expected behavior
 of `fields` from my understanding.

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


More information about the wp-trac mailing list