I am writing the backend for my theme and have 4 custom post types. I have registered them all and now I am writing the metaboxes and handlers.

My question is: in WordPress admin, is there a way of determining which custom post type you're editing? i.e. lets say I have a custom post type of Testimonials, I click Add New and am presented with various fields. Is there a function in WordPress that will tell me what this post type is?

I've tried get_post_type(), but it returns nothing.


There are hooks and variables set that will allow you determine post type pretty easily, so you can hook onto actions specific to a given type, i'm going to provide some examples below.


If wanted to add a metabox to the post creation screen for the book post type, then i'd probably use a hook along the lines of..

add_action( 'add_meta_boxes_book', 'my_callback_defining_metaboxes' );
function my_callback_defining_metaboxes() {
    add_meta_box( ... );

You can replace book with an appropriate post type, eg. post, page, or a custom one.

If i wanted to enqueue scripts onto the edit or add new posts screen for the book post type, i might use..

add_action( 'admin_print_scripts-edit.php', 'my_func_to_enqueue_scripts' );
add_action( 'admin_print_scripts-post-new.php', 'my_func_to_enqueue_scripts' );
function my_func_to_enqueue_scripts() {
    global $typenow;
    if( 'book' == $typenow )
        wp_enqueue_script( ... );

If i wanted to go a step further and hook onto every page that deals with the book post type i'll use a more generic hook and perform conditional logic on one of the admin variables..(as long as you don't hook in really early you can reference these vars reliably).

add_action( 'admin_print_scripts', 'enqueue_scripts_on_specific_posttype_pages' );
function enqueue_scripts_on_specific_posttype_pages() {
    global $parent_file;
    if( 'edit.php?post_type=book' == $parent_file )
        wp_enqueue_script( ... );

$parent_file is always the link or URL of the parent menu item for the given post type, which you'll notice is different to the generic $hook_suffix that's appended to the admin_print_scripts- hook, eg. admin_print_scripts-edit.php ..

The above example would hook the enqueue onto any page for the book post type, that includes the taxonomy management screens.

Hope the above is helpful.

| improve this answer | |

The global $current_screen->post_type is more robust than relying on $_GET['post_type'] or $parent_file.

| improve this answer | |

that's because the post hasn't been saved yet.

try with $_GET['post_type']

| improve this answer | |
  • Thanks That works if I have records. What if I click on Add New, is there a way then of determining the post type. – Steve Clark Dec 23 '10 at 12:09
  • Just answered my own question post_type is a query string value on add new and id is a value on editing. I can assotain what the value is from both of them. – Steve Clark Dec 23 '10 at 12:11
  • that works if you don't have records too – onetrickpony Dec 23 '10 at 12:22
  • That's not true. The post has been created since it is a new record on db but in draft state. Infact you can get the ID of that post type. – guido Apr 2 '17 at 16:35
  • I don't think this works as of wp 5.0 – Jacob Raccuia Apr 26 '19 at 1:40

If you want to get info about the current screen you can but depend in which hook you are working.

For example, have a look at:

add_action('add_meta_boxes', function () {
    $screen = get_current_screen();


When you click on Add New and during the add_meta_boxes is executed you can retrieve the current screen (please don't use global variables) and also the new ID of the post that is currently in Draft state.

| improve this answer | |

Let's close this storry. I create function for solving this problem and you can use it how you want. It is ment for the wp-admin part but you can expand it.

function wp_admin_post_type () {
    global $post, $parent_file, $typenow, $current_screen, $pagenow;

    $post_type = NULL;

    if($post && (property_exists($post, 'post_type') || method_exists($post, 'post_type')))
        $post_type = $post->post_type;

    if(empty($post_type) && !empty($current_screen) && (property_exists($current_screen, 'post_type') || method_exists($current_screen, 'post_type')) && !empty($current_screen->post_type))
        $post_type = $current_screen->post_type;

    if(empty($post_type) && !empty($typenow))
        $post_type = $typenow;

    if(empty($post_type) && function_exists('get_current_screen'))
        $post_type = get_current_screen();

    if(empty($post_type) && isset($_REQUEST['post']) && !empty($_REQUEST['post']) && function_exists('get_post_type') && $get_post_type = get_post_type((int)$_REQUEST['post']))
        $post_type = $get_post_type;

    if(empty($post_type) && isset($_REQUEST['post_type']) && !empty($_REQUEST['post_type']))
        $post_type = sanitize_key($_REQUEST['post_type']);

    if(empty($post_type) && 'edit.php' == $pagenow)
        $post_type = 'post';

    return $post_type;

In the general this will give you the most accurate post type information inside WordPress Admin side.


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy