This code outputs the following warning when run on the pre_get_posts
action:
Trying to get property of non-object
Which points to this line:
if( 'edit' == $screen->base && 'my_post_type' == $screen->post_type && $order_by == '0' ) {
Why am I getting this warning?
public function my_post_type_default_sort( $query ){
if( !is_admin() ) {
return;
}
$screen = get_current_screen();
if( !isset( $_GET['orderby'] ) ) {
$order_by = '0';
}
if( 'edit' == $screen->base && 'my_post_type' == $screen->post_type && $order_by == '0' ) {
$query->set( 'orderby', 'scr_date' );
$query->set( 'order', 'ASC' );
}
}
$screen
and check what it contains at that point?