[wp-trac] Re: [WordPress Trac] #6484: have_posts does not seem to work outside of the loop

WordPress Trac wp-trac at lists.automattic.com
Mon Mar 31 17:19:22 GMT 2008


#6484: have_posts does not seem to work outside of the loop
---------------------+------------------------------------------------------
 Reporter:  jsherk   |        Owner:  anonymous
     Type:  defect   |       Status:  closed   
 Priority:  normal   |    Milestone:  2.5.1    
Component:  General  |      Version:  2.5      
 Severity:  normal   |   Resolution:  invalid  
 Keywords:           |  
---------------------+------------------------------------------------------
Changes (by markjaquith):

  * status:  new => closed
  * resolution:  => invalid

Comment:

 {{{have_posts()}}} doesn't know about {{{$mytest}}}.  {{{have_posts()}}}
 is for checking the main {{{$wp_query}}} WP_Query object.

 Try this:

 {{{
 <?php
 require('wp-blog-header.php');
 $myid=10; //put a post id here that you know is valid
 $wp_query = new WP_Query( array('p' => $myid ) );
 while ( have_posts() ) { the_post(); the_title(); }
 ?>
 }}}

 If you don't want to disturb the main query, do this:

 {{{
 <?php
 require('wp-blog-header.php');
 $myid=10; //put a post id here that you know is valid
 $my_query = new WP_Query( array('p' => $myid ) );
 var_dump(have_posts());
 while ( $my_query->have_posts() ) { $my_query->the_post(); the_title(); }
 ?>
 }}}

-- 
Ticket URL: <http://trac.wordpress.org/ticket/6484#comment:3>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list