Codex

Function Reference/have posts

Contents

Description

This function checks to see if the current WordPress query has any results to loop over. This is a boolean function, meaning it returns either TRUE or FALSE.

At the end of the loop, will automatically call rewind_posts.

Usage

<?php have_posts(); ?>

Parameters

This function does not accept any parameters.

Return Values

(boolean) 
True on success, false on failure.

Examples

The following example can be used to determine if any posts exist, and if they do, loop through them.

<?php
if ( have_posts() ) :
	while ( have_posts() ) : the_post();
		// Your loop code
	endwhile;
else :
	echo wpautop( 'Sorry, no posts were found' );
endif;
?>

Change Log

Source File

have_posts() is located in wp-includes/query.php.

Related

Query Tags: WP_Query (Class), get_query_var(), query_posts(), have posts(), the_post(), rewind_posts(), wp_reset_postdata(), wp_reset_query()

See also index of Function Reference and index of Template Tags.