16

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

2
  • what about global $post?
    – Sisir
    Commented Jul 4, 2014 at 12:56
  • global post is not available in the admin_init hook yet
    – Malibur
    Commented Jul 4, 2014 at 13:15

3 Answers 3

28
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
    }
}
5
  • When editing an existing post the url is '/wp-admin/post.php?post=81&action=edit'
    – Malibur
    Commented Jul 4, 2014 at 13:24
  • Allright fixed now... even though you have to query the db to do so... Commented Jul 4, 2014 at 13:55
  • 2
    Please add an explanation to what your code does Commented Jul 4, 2014 at 13:59
  • A very helpfull answer even in 2018! Commented Apr 23, 2018 at 18:08
  • Code only answers are not very useful. See @PieterGoosen commment above, from ~5 years ago.... Commented Mar 27, 2019 at 18:06
6

get_current_screen()->post_type holds post type slug. Or $_REQUEST['post_type']. Note that for standard pages/posts post_type might not be set.

3

I am going to expand on MiCc83's answer. There are a few things that don't follow the OP's original questions but overall it's a great solution. For example, it would not work with a post_type event because you are checking the post_type as 'post' in the answer.

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 ( 'post.php' === $pagenow && isset($_GET['post']) ){
        $post_id = $_GET['post'];

        // Do something with $post_id. For example, you can get the full post object:
        $post = get_post($post_id);

    }
}

The condition 'post' === get_post_type( $_GET['post'] ) in the previous answer would prevent this from working on a post type 'event'. You would need to check for post type 'event' instead of 'post'.

1
  • 1
    This is a complete and working answer. Commented Dec 9, 2020 at 8:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.