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' );
        }

    }

Read more here: pre_get_posts – Trying to get property of non-object warning


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: