[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
Thu Sep 14 07:27:54 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 lagdonkey):

 Can confirm that in Wp 4.8.1, this issue is still present. What caused me
 to notice, was the fact that on save, the currently edited post/page's
 slug was changing to match the slug of the last post in the Wp_query I was
 running in the post editor. My meta box was hooked into low priority in
 the sidebar section of the editor.

 This solution saved my bacon, but the issue itself cost me about an hour
 of debugging, trying to find the culprit. Issue occurred on my local
 Apache hosted dev environment with no plugins and only a custom theme I
 was working on. If it at all helps, I've also noticed issues in other
 areas of wp's backend when dealing with custom wp_query's, most noticably
 in other plugins like Advanced custom fields.

 Replying to [comment:26 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:27>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list