[wp-trac] [WordPress Trac] #43502: `WP_REST_Posts_Controller::prepare_item_for_response()` doesn't reset postdata after calling setup_postdata()

WordPress Trac noreply at wordpress.org
Sat Mar 10 19:25:20 UTC 2018


#43502: `WP_REST_Posts_Controller::prepare_item_for_response()` doesn't reset
postdata after calling setup_postdata()
------------------------------------------+-----------------------
 Reporter:  gerbenvandijk                 |       Owner:
     Type:  defect (bug)                  |      Status:  new
 Priority:  normal                        |   Milestone:  4.9.5
Component:  REST API                      |     Version:  4.9.4
 Severity:  normal                        |  Resolution:
 Keywords:  needs-patch needs-unit-tests  |     Focuses:  rest-api
------------------------------------------+-----------------------

Comment (by soulseekah):

 The `prepare_item_for_response` method and the controller itself is not
 supposed to be called from within a loop, which is what you seem to be
 doing. You appear to be using a whole REST server for mere data
 transformation, overkill and unintended use ahead :)

 However, it indeed does set the `$post` global and sets the global post
 data up. Should it? Probably not. Does it - yes. Why? `get_the_content()`,
 which doesn't accept an explicit `post_ID` argument, meaning that it will
 only work with a global context setup.

 Should it play nice and reset the `$post` global to what it was? Maybe.

 Should it also call `wp_reset_postdata`? Certainly not. For one it has no
 effect in the REST server context. For two, if it's called from your
 scenario it would actually mess up your loop, resetting too early
 (`wp_reset_postdata` should only be called after the loop has ended,
 right?)

 In my opinion, this should be left alone. The context the REST server is
 being called from is highly atypical. The server is expected to live a
 very short life serving one response per request.

 Thoughts?

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


More information about the wp-trac mailing list