[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
Mon Aug 15 02:38:08 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:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Editor        |    Version:  3.2.1
 Severity:  normal        |   Keywords:  has-patch
--------------------------+-----------------------------
 While on an edit post page (or any post type), if you create a custom
 WP_Query object before the editor has been output, the post data from the
 custom WP_Query will fill out the edit post form, not the original content
 of the post that is actually trying to be edited.

 I found this out when trying to create a custom metabox in the 'side'
 context. You can reproduce this by inserting this code into a plugin:


 {{{

 add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );

 function myplugin_add_custom_box() {
         add_meta_box(
         'myplugin_sectionid',
         __( 'My Post Section Title', 'myplugin_textdomain' ),
         'myplugin_inner_custom_box',
         'post',
         'side'
     );
 }

 function myplugin_inner_custom_box() {
         global $post;
         $a = new WP_Query('post_type=page');
         while($a->have_posts() ) : $a->the_post();
         endwhile;
         wp_reset_postdata();
 }
 }}}

 This happens because $wp_query->post is never defined in the admin load,
 which wp_reset_postdata relies on to reset the original post data.

 I am attaching a patch that defines $wp_query->post after the $post global
 has been defined.

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


More information about the wp-trac mailing list