Skip to content

Instantly share code, notes, and snippets.

@bradvin
Forked from mjangda/get_current_post_type.php
Created March 5, 2012 18:48
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save bradvin/1980309 to your computer and use it in GitHub Desktop.
Save bradvin/1980309 to your computer and use it in GitHub Desktop.
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;
}
?>
@DomenicF
Copy link

@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