[wp-trac] [WordPress Trac] #11698: have_posts should not auto rewind

WordPress Trac wp-trac at lists.automattic.com
Sat Jan 2 23:47:14 UTC 2010


#11698: have_posts should not auto rewind
--------------------------+-------------------------------------------------
 Reporter:  mwillberg     |       Owner:            
     Type:  defect (bug)  |      Status:  new       
 Priority:  normal        |   Milestone:  Unassigned
Component:  General       |     Version:            
 Severity:  minor         |    Keywords:            
--------------------------+-------------------------------------------------
 When the loop has been exhausted the have_posts will auto rewind itself.
 ALL documentation and examples are not valid as they claim that
 have_posts() will return false when there are no more posts in query.

 Example, this will incorrectly go thru the loop twice:

 {{{
 while (have_posts()) {the_post();the_title();}
 while (have_posts()) {the_post();the_title();}
 }}}


 the later loop should not do anything as the query has already been walked
 thru.


 Suggested FIX, to remove the rewind_posts();

 {{{
 query.php / function have_posts() {
 ...
 do_action_ref_array('loop_end', array(&$this));
 // Do some cleaning up after the loop
 // $this->rewind_posts();
 ...
 }
 }}}

 Life after the fix:

 If someone really wants to revert to old behaviour, it is already possible
 to do easily. So AFTER the issue has been fixed (ie. rewinding has been
 removed):

 {{{
 while (have_posts()) {the_post();}

 if (!have_posts()) {rewind_posts();}
 while (have_posts()) {the_post();}
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/11698>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list