How do i know the current post type when on post.php in admin?

Im trying to do something with an admin_init hook if – and only if – the user is editing a post (post.php) with post type “event”. My problem is that, even though wordpress points to a global variable calls $post_type. if i do:

global $post_type;
var_dump($post_type);

It returns NULL.

but if I do this:

global $pagenow;
var_dump($pagenow);

it returns my current page. i.e. “post.php”.

I looked into this function $screen = get_current_screen(); but thats not declared until after the admin_init hooks has run, and then its to late.

So my question is, how do I, by the time admin_init is run, find out what post type the current post being edited is. if the url is post.php?post=81&action=edit then, how do I know what post type postid=81 is?

Thanks
Malthe

3

add_action( 'admin_init', 'do_something_152677' );
function do_something_152677 () {
    // Global object containing current admin page
    global $pagenow;

    // If current page is post.php and post isset than query for its post type 
    // if the post type is 'event' do something
    if ( 'post.php' === $pagenow && isset($_GET['post']) && 'post' === get_post_type( $_GET['post'] ) )
        // Do something
    }
}

Leave a Comment