Title: have_posts
Published: April 25, 2014
Last modified: February 24, 2026

---

# have_posts(): bool

## In this article

 * [Return](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#wp--skip-link--target)

Determines whether current WordPress query has posts to loop over.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#return)󠁿

 bool True if posts are available, false if end of the loop.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#more-information)󠁿

This function checks whether there are more posts available in the main [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
object to loop over. It calls `[have_posts()](https://developer.wordpress.org/reference/classes/wp_query/have_posts/)`
method on the global `$wp_query` object.

If there are no more posts in the loop, it will trigger the [`loop_end`](https://developer.wordpress.org/reference/hooks/loop_end/)
action and then call call [`rewind_posts()`](https://developer.wordpress.org/reference/classes/wp_query/rewind_posts/)
method.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#source)󠁿

    ```php
    function have_posts() {
    	global $wp_query;

    	if ( ! isset( $wp_query ) ) {
    		return false;
    	}

    	return $wp_query->have_posts();
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/query.php#L941)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/query.php#L941-L949)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Query::have_posts()](https://developer.wordpress.org/reference/classes/wp_query/have_posts/)`wp-includes/class-wp-query.php` |

Determines whether there are more posts available in the loop.

  |

| Used by | Description | 
| [get_the_block_template_html()](https://developer.wordpress.org/reference/functions/get_the_block_template_html/)`wp-includes/block-template.php` |

Returns the markup for the current template.

  | 
| [WP_Media_List_Table::has_items()](https://developer.wordpress.org/reference/classes/wp_media_list_table/has_items/)`wp-admin/includes/class-wp-media-list-table.php` |  | 
| [WP_Media_List_Table::display_rows()](https://developer.wordpress.org/reference/classes/wp_media_list_table/display_rows/)`wp-admin/includes/class-wp-media-list-table.php` |

Generates the list table rows.

  | 
| [WP_Posts_List_Table::has_items()](https://developer.wordpress.org/reference/classes/wp_posts_list_table/has_items/)`wp-admin/includes/class-wp-posts-list-table.php` |  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#comment-content-851)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/have_posts/#comment-851)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-851)
     Vote results for this note: 4[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-851)
 4.  **Default use:**
      The following example can be used to determine if any posts 
     exist and loop through them if they do.
 5.      ```php
         if ( have_posts() ) :
             while ( have_posts() ) : the_post();
                 // Your loop code
             endwhile;
         else :
             _e( 'Sorry, no posts were found.', 'textdomain' );
         endif;
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%3Freplytocom%3D851%23feedback-editor-851)
 7.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#comment-content-841)
 8.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/have_posts/#comment-841)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-841)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-841)
 10. **Avoiding infinite loops:**
      Calling this function within the loop will cause
     an infinite loop. For example, see the following code:
 11.     ```php
         while ( have_posts() ) : the_post();
             // Display post
             if ( have_posts() ) : // If this is the last post, the loop will start over
                 // Do something if this isn't the last post
             endif;
         endwhile;
         ```
     
 12. If you want to check if there are more posts in the current loop without this 
     unfortunate side effect, you can use this function:
 13. In your `functions.php` file:
 14.     ```php
         /**
          * Check if a loop has any more posts left.
          *
          * @global $wp_query
          *
          * @return bool True if there are any more posts in this loop, false if not.
          */
         function wpdocs_has_more_posts() {
           global $wp_query;
           return $wp_query->current_post + 1 < $wp_query->post_count;
         }
         ```
     
 15. In your template file:
 16.     ```php
         while ( have_posts() ) : the_post();
             // Display post
             if ( wpdocs_has_more_posts() ) :
                 // Do something if this isn't the last post
             endif;
         endwhile;
         ```
     
 17.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%3Freplytocom%3D841%23feedback-editor-841)
 18.  [Skip to note 7 content](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#comment-content-5435)
 19.   [Muhammad Jawad Abbasi](https://profiles.wordpress.org/jawad1234/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/have_posts/#comment-5435)
 20. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-5435)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-5435)
 21.     ```php
         <?php if ( have_posts() ) : while( have_posts()  ) : the_post(); ?>
             <h1><?php the_title(); ?></h1>
             <a href="<?php the_permalink(); ?>">
                 <?php the_post_thumbnail( 'full' ); ?>
             </a>
             <p><?php the_excerpt(); ?></p>
         <?php endwhile; endif; ?>
         ```
     
 22. **Output:**
      Post title Post featured full size image (When anyone click on the
     image then open post single page) Post small description
 23. _Edited by @audrasjb: Small WPCS fixes._
 24.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%3Freplytocom%3D5435%23feedback-editor-5435)
 25.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/have_posts/?output_format=md#comment-content-5800)
 26.   [johnhawley](https://profiles.wordpress.org/johnhawley/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/have_posts/#comment-5800)
 27. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-5800)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%23comment-5800)
 28. **Example:** if creating a custom [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
     out of the standard WP loop context. This example uses `have_posts()` function
     extended from `WP_Query`:
 29.     ```php
         <?php
         // Define args
         $args = array('post_type' => 'custom_post_type');
     
         // Execute query
         $cpt_query = new WP_Query($args);
     
         // Create cpt loop, with a have_posts() check!
         if ($cpt_query->have_posts()) :
           while ($cpt_query->have_posts()) : $cpt_query->the_post(); ?>
     
             <?php the_title(); ?>
     
         <?php endwhile;
         endif; ?>
         ```
     
 30.  * Infinite loop? Loop that hangs your website? Look at the section about Infinite
        Loops then scan through the other solutions (that didn’t work for me) and finally
        I came up John Hawley’s comment and that works for me. Thanks to John for posting!
      * [provopeople](https://profiles.wordpress.org/provopeople/) [2 years ago](https://developer.wordpress.org/reference/functions/have_posts/#comment-7174)
 31.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F%3Freplytocom%3D5800%23feedback-editor-5800)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhave_posts%2F)
before being able to contribute a note or feedback.