Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the current post_type context in the WordPress admin.
<?php
/**
* gets the current post type in the WordPress Admin
*/
function get_current_post_type() {
global $post, $typenow, $current_screen;
//we have a post so we can just get the post type from that
if ( $post && $post->post_type )
return $post->post_type;
//check the global $typenow - set in admin.php
elseif( $typenow )
return $typenow;
//check the global $current_screen object - set in sceen.php
elseif( $current_screen && $current_screen->post_type )
return $current_screen->post_type;
//lastly check the post_type querystring
elseif( isset( $_REQUEST['post_type'] ) )
return sanitize_key( $_REQUEST['post_type'] );
//we do not know the post type!
return null;
}
?>
@noahjwhitmore

This comment has been minimized.

Copy link

noahjwhitmore commented Dec 14, 2012

As of WP 3.3, none of this is necessary. Just use the get_current_screen function.

http://codex.wordpress.org/Function_Reference/get_current_screen

id; if ($post_type == 'your-post-type-here') { do something } else { do something else } ?>
@TandFMachine

This comment has been minimized.

Copy link

TandFMachine commented Feb 15, 2014

get_current_screen will return null when used in the admin_init hook, whereas the above Gist works at any point in the code execution.

@yratof

This comment has been minimized.

Copy link

yratof commented Mar 10, 2015

WorksThis really does work like a charm. Trying to enqueue scripts on a specific admin cpt, this was the only thing i've found that worked.

@hasnatbabur

This comment has been minimized.

Copy link

hasnatbabur commented Feb 15, 2016

Not working for me :(

if( 'album' == pj_get_current_post_type() ){
// yoast seo metabox priority low
add_filter('wpseo_metabox_prio', 'pj_wpseo_metabox_prio_callback');
function pj_wpseo_metabox_prio_callback(){
return 'default';
}
}

@DomenicF

This comment has been minimized.

Copy link

DomenicF commented May 25, 2016

@bradvin I updated this gist, see if you like my changes and want to edit yours: https://gist.github.com/DomenicF/3ebcf7d53ce3182854716c4d8f1ab2e2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.