[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
Fri Feb 24 17:18:24 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 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.

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


More information about the wp-trac mailing list