WP_Query::have_posts(): bool
- Since
- 1.5.0
- Source
wp-includes/class-wp-query.php:3827
Check whether a WP_Query loop has posts left with have_posts(); true while posts remain, false at the end, when it also fires the loop_end action. Together with the_post() it drives the WordPress loop, for the main query via the global functions or directly on a custom WP_Query.
Description
Calls the 'loop_end' action when the loop is complete.
Compatibility
- WordPress
- since 1.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
bool- True if posts are available, false if end of the loop.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Loop over a custom WP_Query
have_posts() advances nothing by itself; it reports whether the_post() has anything left to set up.
$recent = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 3,
) );
if ( $recent->have_posts() ) {
while ( $recent->have_posts() ) {
$recent->the_post();
echo '- ', get_the_title(), ' (', get_permalink(), ")\n";
}
wp_reset_postdata();
} else {
echo 'No posts matched.';
}
echo "\nposts returned: ", $recent->post_count, ' of ', $recent->found_posts, ' found';wp_reset_postdata() restores the global $post that the_post() overwrote.
Performance profile
How much work a call to WP_Query::have_posts() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 6–26
- Plugin surface
- 2 hooks
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 36.
Third-party callbacks on 'loop_end', 'loop_no_results' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action_ref_array()called directly
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::have_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–19 | none |
| always | 23–26 | do_action() |
| always | 25 | ->rewind_posts() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 36 instructions, 6–26 executed per call, 4 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while WP_Query::have_posts() runs, in this order:
- do_action( loop_no_results )actionline 3852 (+25 into the body)
Fires if no results are found in a post query.
Uses · 3
- do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Query::rewind_posts()Rewinds the posts and resets post index.
Source code
public function have_posts() { if ( $this->current_post + 1 < $this->post_count ) { return true; } elseif ( $this->current_post + 1 === $this->post_count && $this->post_count > 0 ) { /** * Fires once the loop has ended. * * @since 2.0.0 * * @param WP_Query $query The WP_Query instance (passed by reference). */ do_action_ref_array( 'loop_end', array( &$this ) ); // Do some cleaning up after the loop. $this->rewind_posts(); } elseif ( 0 === $this->post_count ) { $this->before_loop = false; /** * Fires if no results are found in a post query. * * @since 4.9.0 * * @param WP_Query $query The WP_Query instance. */ do_action( 'loop_no_results', $this ); } $this->in_the_loop = false; return false; }Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/class-wp-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.