have_posts()
Whether current WordPress query has results to loop over.
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. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
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:
Default use:
The following example can be used to determine if any posts exist and loop through them if they do.