Title: Check if next post exist.
Last modified: August 19, 2016

---

# Check if next post exist.

 *  [cyberek0](https://wordpress.org/support/users/cyberek0/)
 * (@cyberek0)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/check-if-next-post-exist/)
 * I need to check, if in the loop next post exist.
    Someone adviced me to use this:
 *     ```
       <?php $my_query = new WP_Query('category_name=portfolio&showposts=9999'); ?>
       <?php while ($my_query->have_posts()) : $my_query->the_post(); $i++; ?>					
   
       	<?php the_title(); ?>
       	<?php if ($my_query->have_posts()) : ?>
       		<hr />
       	<?php endif; ?>		
   
       <?php endwhile; ?>
       ```
   
 * But this solution makes the loop goes infinity (afret first pass on all posts,
   it goes over again).
    So what is the method to check if the next post exist, 
   and if so, then do something (display hr in example)?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/check-if-next-post-exist/#post-1259098)
 * Put this after your new WP_Query
 *     ```
       echo "<pre>"; print_r($my_query->post_count); echo "</pre>";
       ```
   
 *  Thread Starter [cyberek0](https://wordpress.org/support/users/cyberek0/)
 * (@cyberek0)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/check-if-next-post-exist/#post-1259174)
 * I have got to this method after some try and fail approaches.
    Finally comparing
   $i to post_count, so it does the job as far, as i have all posts on one page.
   But, what if pagination start? Can i access default posts/page to compare to 
   it? My solution works like this:
 *     ```
       <?php if( ($my_query->post_count) == $i ) : ?>
       <hr />
       <?php endif; ?>
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/check-if-next-post-exist/#post-1259191)
 * Don’t have 9999 posts to show a paging example….use this to look at all the query
   variables:
 *     ```
       echo "<pre>"; print_r($my_query->query_vars); echo "</pre>";
       ```
   
 * You’ll want
    $my_query->post_per_page $my_query->paged

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Check if next post exist.’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/check-if-next-post-exist/#post-1259191)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
