Opened 18 months ago
#63106 new defect (bug)
The loop can throw warning if `WP_Query::have_posts()` not called.
| Reported by: | peterwilsoncc | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | Awaiting Review |
| Component: | Query | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
Starting or overrunning the loop can throw a warning if WP_Query::have_posts() is not called correctly via the developer. The issue can be seen in these tests:
<?php function test_no_posts() { $query = new WP_Query( array( 'post_type' => 'unregistered_cpt' ) ); $query->the_post(); $this->assertEmpty( $query->posts ); } function test_too_many_calls() { $post_id = $this->factory()->post->create(); $query = new WP_Query( array( 'p' => $post_id ) ); // Call the post twice. $query->the_post(); $query->the_post(); $this->assertNotEmpty( $query->posts ); }
In each case the tests will throw an error before reaching the assertion, as they are attempting to access an array key that does not exist.
A part of me is tempted to close this as wontfix, as it's very clearly documented that one should use while ( have_posts() ) for the primary loop or while $query->have_posts() for a secondary loop.
Note:
See TracTickets
for help on using tickets.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)