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

WordPress Trac noreply at wordpress.org
Mon Apr 24 19:23:57 UTC 2017


#18408: Can't wp_reset_postdata after custom WP_Query in an admin edit page
-----------------------------------------------+---------------------------
 Reporter:  ericlewis                          |       Owner:  ericlewis
     Type:  defect (bug)                       |      Status:  reopened
 Priority:  normal                             |   Milestone:  Future
Component:  Query                              |  Release
 Severity:  normal                             |     Version:  4.7.2
 Keywords:  needs-testing ongoing needs-patch  |  Resolution:
                                               |     Focuses:
-----------------------------------------------+---------------------------

Comment (by nitesh.luharuka):

 This works perfectly. The problem persists with 4.7.4 as well.

 Replying to [comment:25 jpSimkins]:
 > To bypass this issue, do not use `the_post` as this is the issue.
 >
 > I use this syntax and I have numerous metaboxes with no issue to the
 default post object nor the need to call `wp_reset_postdata();`.
 >
 > {{{#!php
 > <?php
 > $query = new \WP_Query($args);
 > if ($query->have_posts()) {
 >     foreach ($query->get_posts() as $p) {
 >         // nomal loop logic using $p as a normal WP_Post object
 >     }
 > }
 > }}}
 >
 > It needs to be noted that you cannot use the loop methods like
 `get_the_title()` but you can easily call `$p->post_title`. I do not
 believe the filters are applied to these so you may need to use a
 different object for the backend than the frontend since the issue is only
 on the backend of WP.
 >
 > Also, you could just use the same args with `get_posts();`

--
Ticket URL: <https://core.trac.wordpress.org/ticket/18408#comment:26>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list