0

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

1 Answer 1

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);
    }
}
5
  • Not working throws a Fatal error. PHP Fatal error: Call to undefined function current_screen(). Also I required once the screen.php require_once(ABSPATH . 'wp-admin/includes/screen.php'); Commented Aug 7, 2015 at 5:19
  • Testted, works fine for me. Don't require screen.php, it's already loaded. Commented Aug 7, 2015 at 6:07
  • I need it to work with the parse_query action hook not the current_screen action hook. Commented Aug 10, 2015 at 5:15
  • See my edit. Tested, works. Commented Aug 10, 2015 at 5:39
  • Why is this happening on my Windows computer (using WAMP) and not on my Mac (using MAMP) ? Commented Jun 24, 2019 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.