[wp-trac] [WordPress Trac] #36097: wp_filter reset after sub-queries

WordPress Trac noreply at wordpress.org
Mon Mar 7 04:03:44 UTC 2016


#36097: wp_filter reset after sub-queries
-------------------------------+------------------------------
 Reporter:  cartpauj           |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Query              |     Version:  4.4.2
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------
Changes (by boonebgorges):

 * keywords:   => reporter-feedback


Comment:

 I'm not able to reproduce the issue. The filter is being applied to each
 instance of `the_content()` within the shortcode query, as well as the
 content of the post that contains the shortcode. I've used the exact code
 from above (but filled in some args for the `WP_Query` instance), in the
 context of an mu-plugin. Have I done something wrong?

 It may not be related, but it's odd and unnecessary to assign your
 shortcode's query to the `$wp_query` global. `global $wp_query` is
 supposed to contain the main query. The following should be sufficient:

 {{{
 $my_query = new WP_Query( $args );
 while ( $my_query->have_posts() ) {
     $my_query->the_post();

     // ...
 }

 // wp_reset_query() is not necessary if you're not touching $wp_query
 wp_reset_postdata();
 }}}

 > When the do_shortcode call is finished, all of the filters for
 the_content ($wp_filter) have already run, so any after that don't get
 called on the main content.

 Not exactly. Shortcodes are parsed like this:

 `add_filter( 'the_content', 'do_shortcode', 11 );`

 Consider a post with the content "Main Post Content [some-custom-loop]".
 This will get run through `append_to_content()` first, at priority 10:
 "Main Post Content [some-custom-loop]<p>Hello WP!</p>". Then, at priority
 11, `do_shortcode()` will run `my_shortcode_callback()` and replace
 "[some-custom-loop]" with the content of your output buffer: "Main Post
 Content `$output_buffer` <p>Hello WP!</p>". This is, in fact, what I'm
 seeing in my tests.

 To put the point a different way: 'the_content' callbacks, once registered
 via `apply_filters()`, are not removed from the `$wp_filter` array unless
 it's done manually via `remove_filter()`. The callbacks will be invoked
 each time `the_content()` is called. While it's true that you're running a
 `WP_Query` inside of another `WP_Query`, the calls to `the_content()` are
 not themselves nested in any way that's relevant here. By the time
 `the_content()` is called in your shortcode handler, `append_to_content()`
 has already been applied to the content of the container post.

 Again, it could be that I've missed a critical part of the setup, or it
 could be that you've got a rogue plugin doing something funky with
 `remove_filters()`. But WP shouldn't be demonstrating this behavior out of
 the box, and it's not for me.

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


More information about the wp-trac mailing list