[wp-trac] [WordPress Trac] #18408: Can't wp_reset_postdata after custom WP_Query in an admin edit page

WordPress Trac wp-trac at lists.automattic.com
Tue Nov 15 16:00:14 UTC 2011


#18408: Can't wp_reset_postdata after custom WP_Query in an admin edit page
--------------------------------------------------+------------------------
 Reporter:  ericlewis                             |       Owner:  ericlewis
     Type:  defect (bug)                          |      Status:  closed
 Priority:  normal                                |   Milestone:
Component:  Editor                                |     Version:  3.2.1
 Severity:  normal                                |  Resolution:  invalid
 Keywords:  has-patch needs-testing dev-feedback  |
--------------------------------------------------+------------------------

Comment (by billerickson):

 Regardless of what method you use to generate the loop, if you set the
 post data to something else you'll need to reset it. Using get_posts:


 {{{
 $original_post = $post;
 $myposts = get_posts( $args );
 foreach( $myposts as $post ) :  setup_postdata($post);
 ...
 endforeach:
 setup_postdata( $original_post);
 }}}


 Using WP_Query (how I'm doing it):


 {{{
 $original_post = $post;
 $loop = new WP_Query( $args );
 while( $loop->have_posts() ): $loop->the_post();
 ...
 endwhile;
 setup_postdata( $original_post );
 }}}

 I think it's reasonable to assume that most of the time you're doing a
 query in a metabox, you'll need access to the post data.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18408#comment:6>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list