Changes to post globals setup and usage in get_the_content() and related functions in WordPress 5.2

In WordPress 4.5, changes were made to some post template functions (specifically, get_the_excerpt() and related functions) to improve their usability outside of the post loop; see #27246. Since that release, some users have noticed that these changes had certain side effects, specifically when the template functions incorrect used the post-related global variables ($authordata, $page, etc) as fallbacks.

WordPress 5.2 addresses these inconsistencies by relying less on post-related globals in functions like get_the_excerpt(), get_the_content(), and wp_trim_excerpt(). Where possible, these functions now prefer the $post value explicitly passed as a function argument; and the main WordPress loop setup has been updated to pass the correct post to the functions wherever possible. See #36934, #42814, [44941].

Aside from greater predictability when using the functions outside the post loop, it’s not expected that these changes will have any visible effect in the vast majority of cases. Developers should note one potentially breaking change: The 'content_pagination' filter now runs before the post globals are populated, which may be a compatibility break in some cases. Callbacks for 'content_pagination' are thus urged to use the $post parameter passed to the filter rather than relying on the globals. See #47133.

More generally, with respect to template functions that are designed for use inside the WordPress loop, developers should take special care when using them outside the context of a loop. setup_postdata(), as always, sets up many of the post-related globals that may be expected by other template functions, but it does not change the value of the $post global. See https://developer.wordpress.org/reference/functions/setup_postdata/#comment-874. Be sure to test compatibility with plugins and themes you’re using before using these template functions outside the loop.

#dev-notes