On this page
WP_Query::have_posts(): bool
Determines whether there are more posts available in the loop.
Description
Calls the ‘loop_end’ action when the loop is complete.
Return
bool True if posts are available, false if end of the loop.
Source
File: wp-includes/class-wp-query.php. View all references
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 ) {
/**
* 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;
}
Hooks
- do_action_ref_array( 'loop_end',
WP_Query $query ) -
Fires once the loop has ended.
- do_action( 'loop_no_results',
WP_Query $query ) -
Fires if no results are found in a post query.
Related
Uses
| Uses | Description |
|---|---|
| WP_Query::rewind_posts() wp-includes/class-wp-query.php | Rewind the posts and reset post index. |
| do_action_ref_array() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook, specifying arguments in an array. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
Used By
| Used By | Description |
|---|---|
| get_feed_build_date() wp-includes/feed.php | Gets the UTC time of the most recently modified post from WP_Query. |
| have_posts() wp-includes/query.php | Determines whether current WordPress query has posts to loop over. |
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_query/have_posts