[wp-trac] [WordPress Trac] #55917: Fatal error in WP6.0 when calling get_current_screen on the pre_get_posts action
WordPress Trac
noreply at wordpress.org
Sat Jun 4 10:37:52 UTC 2022
#55917: Fatal error in WP6.0 when calling get_current_screen on the pre_get_posts
action
----------------------------+-----------------------------
Reporter: lumpysimon | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Administration | Version: 6.0
Severity: normal | Keywords:
Focuses: administration |
----------------------------+-----------------------------
In 5.9 and earlier versions, it's possible to call `get_current_screen`
from within a function that runs on the `pre_get_posts` hook. But in
version 6.0 it causes a fatal error `Call to undefined function
get_current_screen()`.
{{{
add_action( 'pre_get_posts', 'abcdef_sort_posts' );
function abcdef_sort_posts( $query ) {
if ( wp_doing_ajax() ) {
return;
}
if ( ! is_admin() ) {
return;
}
$screen = get_current_screen();
if ( 'edit-page' == $screen->id ) {
$query->set( 'orderby', 'post_title' );
$query->set( 'order', 'DESC' );
}
}
}}}
I came across the issue on two sites, I can reproduce it by installing 5.9
with no plugins enabled, adding the above code in a plugin or
`functions.php`, everything works fine, then upgrading to 6.0 and visiting
any admin screen causes the fatal error. Front-end is fine.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/55917>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list