wp admin - get_current_screen - 'Trying to get property of non-object' error

admin2025-01-07  6

Hello im using this code in the admin to filter custom post types edit.php page

but get_current_screen $screen->post_type is throwing a php notice.

Notice: Trying to get property of non-object

add_action( 'parse_query', 'filtering_edit_table' );

function filtering_edit_table( $query ) {

$screen = get_current_screen();

if( is_admin() && $screen->post_type == 'custom_post_type' && $screen->base == 'edit' ) {
           // Do some things     
            }

}

How can I fix this?

Edit:

I am already requiring screen.php which contains get_current_screen()

require_once(ABSPATH . 'wp-admin/includes/screen.php');

Hello im using this code in the admin to filter custom post types edit.php page

but get_current_screen $screen->post_type is throwing a php notice.

Notice: Trying to get property of non-object

add_action( 'parse_query', 'filtering_edit_table' );

function filtering_edit_table( $query ) {

$screen = get_current_screen();

if( is_admin() && $screen->post_type == 'custom_post_type' && $screen->base == 'edit' ) {
           // Do some things     
            }

}

How can I fix this?

Edit:

I am already requiring screen.php which contains get_current_screen()

require_once(ABSPATH . 'wp-admin/includes/screen.php');

Share Improve this question edited Aug 7, 2015 at 5:21 Bhargav asked Aug 6, 2015 at 5:16 BhargavBhargav 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I tested this code and it worked fine. Where are you requireing screen.php? Same way as this?

require_once(ABSPATH . 'wp-admin/includes/screen.php');
add_action( 'parse_query', 'filtering_edit_table' );

function filtering_edit_table( $query ) {
    $screen = get_current_screen();

    if( is_admin() && $screen->post_type == 'custom_post_type' && $screen->base == 'edit' ) {
        print_r($screen);
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256325a352.html

最新回复(0)