[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:00:12 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 peterwilsoncc):

 Replying to [comment:11 SirLouen]:
 > @peterwilsoncc maybe priming the cache in the next_post is worth, but
 maybe with a use case you can prove why the test
 `test_the_loop_when_querying_post_parents_only`. Have you found an
 scenario where you need more info required by `next_post`?
 >
 > Once you can get `id=>parent` for the part ID, can you just query the ID
 to get the information?

 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:

 {{{
 Failed asserting that two arrays are equal.
 --- Expected
 +++ Actual
 @@ @@
      'ID' => 5
      'ancestors' => Array (...)
      'comment_count' => '0'
 -    'comment_status' => 'closed'
 +    'comment_status' => 'open'
      'filter' => 'raw'
 -    'guid' => 'http://example.org/?page_id=5'
 +    'guid' => ''
      'menu_order' => 0
      'page_template' => ''
 -    'ping_status' => 'closed'
 +    'ping_status' => 'open'
      'pinged' => ''
      'post_author' => '0'
      'post_category' => Array ()
 -    'post_content' => 'Post content 0000023'
 +    'post_content' => ''
      'post_content_filtered' => ''
 -    'post_date' => '2025-02-26 22:47:51'
 -    'post_date_gmt' => '2025-02-26 22:47:51'
 -    'post_excerpt' => 'Post excerpt 0000023'
 +    'post_date' => '0000-00-00 00:00:00'
 +    'post_date_gmt' => '0000-00-00 00:00:00'
 +    'post_excerpt' => ''
      'post_mime_type' => ''
 -    'post_modified' => '2025-02-26 22:47:51'
 -    'post_modified_gmt' => '2025-02-26 22:47:51'
 -    'post_name' => 'post-title-0000023'
 +    'post_modified' => '0000-00-00 00:00:00'
 +    'post_modified_gmt' => '0000-00-00 00:00:00'
 +    'post_name' => ''
      'post_parent' => 4
      'post_password' => ''
      'post_status' => 'publish'
 -    'post_title' => 'Post title 0000023'
 -    'post_type' => 'page'
 +    'post_title' => ''
 +    'post_type' => 'post'
      'tags_input' => Array ()
      'to_ping' => ''
  )
 }}}



 > Can you suggest an scenario where actually retrieving the whole info
 from next post could be useful (instead of simply being able to query the
 next post). I'm assuming that the least information picked, the better
 (specially because the next post is queried for each post in the loop so
 as we were commenting this could be a performance issue).
 >
 > Also, I don't really understand what you mean with priming the cache and
 not sure if still, fetching the whole info from next post, and caching,
 would be a great idea either.
 >
 > Without an example of why this could be useful, I'm clueless of why this
 change is worthy, and despite the patch solves the issue, I'm not sure,
 if, in first place, there is a issue.

 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.

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


More information about the wp-trac mailing list