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 The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post. https://codex.wordpress.org/The_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 Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. 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