have_posts()
Whether current WordPress query has results to loop over.
Contents
Description Description
Return Return
(bool)
Source Source
File: wp-includes/query.php
function have_posts() { global $wp_query; return $wp_query->have_posts(); }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
1.5.0 | Introduced. |
More Information More Information
This function checks whether there are more posts available in the main WP_Query object to loop over. It calls have_posts()
method on the global $wp_query
object.
If there are no more posts in the loop, it will trigger the loop_end
action and then call call rewind_posts()
method.
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Default use:
The following example can be used to determine if any posts exist and loop through them if they do.
Avoiding infinite loops:
Calling this function within the loop will cause an infinite loop. For example, see the following code:
If you want to check if there are more posts in the current loop without this unfortunate side effect, you can use this function:
In your
functions.php
file:In your template file: