If we look at have_posts()
in source, we can see why this happens:
function have_posts() {
if ( $this->current_post + 1 < $this->post_count ) {
return true;
} elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
do_action_ref_array('loop_end', array(&$this));
// Do some cleaning up after the loop
$this->rewind_posts();
}
$this->in_the_loop = false;
return false;
}
When it reaches the end of the loop, it calls rewind_posts
, which resets current_post
back to -1
, so the next call to have_posts
will return true again.
function rewind_posts() {
$this->current_post = -1;
if ( $this->post_count > 0 ) {
$this->post = $this->posts[0];
}
}
Related Posts:
- Query posts only with featured image
- Order by optional meta key?
- get_query_var function not working at all
- get_query_var( ‘paged’ ) not working outside of homepage
- ‘Trying to get property of non-object’ when using WP_Query with ‘fields’ => ‘ids’
- Order by Category and Post in WP custom Query
- Execute a large WP_Query with many “AND” Meta_Queries?
- RSS feed with specific keyword
- Get All IDs Of A Post Type Using WP_Query
- How to know which one is the main query?
- Multiple endpoints in one URL
- WP_Query can’t exclude more than 1 author?
- How to query using a combination of custom_field values?
- Get term by custom term meta and taxonomy
- Generate a tabbed submenu — from taxonomy term or submenu item — with sample content
- posts2posts query using connected_items array issue [closed]
- How to show the posts of some category first, and then all other
- How to detect if a function has been fired on any page so scripts/styles can be loaded conditionally
- Why execute the_post()?
- Count total number of post in foreach loop
- WP_Query not retrieving by random order under parent
- Get images only if parent status is publish
- ACF Relationship + WP Template Parts
- WP_Query Ignoring `post_type` parameter
- Using WP_Query and WP_Meta_Query Outside of WordPress
- How to get the WC_Product Object when using a WP_Query in WooCommerce
- Custom Search Result Page displaying only 1 result.
- How to query a custom post type with a taxonomy filter but display post type archive page?
- How to display 7 most recent days of posts?
- WP_Query Variable inside Array
- Create A Loop With A Variable Number of Posts For Each Bootstrap Row?
- Set Transient does nothing
- Get amount of CPT with a certain custom field value
- query offset countdown by 2 each day?
- Include tags in WP_Query
- How do I make a meta_query OR relation work? [closed]
- WordPress multisite – fallback on subsites to main site on 404 and dealing with switch_to_blog function
- Count author posts in certain category
- How to use wp_query to post count by search term > group post count by date > convert to json. (ex. result 12-21-2020 – 343)
- Taxonomy Query Relation field not behaving correctly?
- Filter posts in category archive page by year using a dropdown
- Woocommerce – Changing the order of the upsell products [closed]
- Custom Query by Author field and meta
- How to output topics and replies in bbpress as table?
- twentyeleven_content_nav hook no longer outputs navigation link after 3.5 upgrade
- how to access query string in wordpress?
- Better wordpress attachment query than this
- How to number the posts (ie #1, #2, #3) in a wp_query?
- How to get all post except particular category without breaking the paging
- How do I rewrite this loop as a new WP_Query style-loop?
- Pagination only won’t work in author template
- How can I check for a thumbnail in WordPress?
- Can’t sort custom post type by random
- WP_Query gives me different results depending on the category order
- Make a SQL query with wpdb in WordPress
- Use WP_Query or query_posts() or get_posts() for optimizing a site?
- exclude product with available tag
- WP_Query meta value order is not working
- Add parameter to first post only
- relation OR instead of AND – Filtered term ID’s in loop
- How to get specific string in explode array?
- using nav menu id’s
- Ignore image urls in wp_query search
- filter posts by meta key with pagination
- Single post – display list of all posts + specific post
- Modify output of custom loop using WP_query
- What’s wrong with this wpdb query?
- The Events Calendar, wp_query returning event date not published date
- meta_query where the meta value is not the post title
- SQL query injection with fifu image
- How do I use multiple similar queries with WP_Query on the same page?
- WP_Query order by multiple meta keys & fields
- Use dedicated functions
- WPQuery Date and ACF
- Is WP Query skipping to check next post when an argument is not met?
- Save large WP_Query to transient === false
- 3 posts from each existing category on one page
- How to show specify category template for both parent and child category
- How to query most recent post for each author?
- WordPress search page pagination displaying but not functional
- query all posts published by certain user id
- How to create URL parameters to run custom queries?
- Query custom meta value – post view and date meta
- WP_Query causing links to not work
- Query Top Set Custom Taxonomy In Given Timeperiod
- Custom taxonomy and query multi conditions
- Orderby Date and Category
- WP_query issue with no posts
- Pagination for custom query throws 404 errors on last pages [duplicate]
- Order posts by custom column using pre_get_posts
- wp_query in form method of widget breaks customizer
- Featured image not showing on page
- Add filter to Orderby Parameter using Array
- Iterate through ID’s in loop
- Need help setting up a search form for wordpress [duplicate]
- problem in query_vars parameters
- Limit Tags display and ad Drop Down Menu
- Query string form $_GET[‘value’] is not working as meta value in wp_query
- Add banner after the third post [closed]
- How to efficiently find “duplicate” posts, where the titles are different, using metadata to match posts?