Themergency fooplugins.com

WordPress Tip - Get Post Type in Admin

| 12 Comments

I was busy writing a plugin today and was needing to know which post type I was dealing with in the WordPress admin. I must have done this a dozen times before and I seem to write it slightly differently every time, so I set out to find a single function that I can use going forward.

And thanks to mjangda and his gist, that is just what I found! I changed it slightly and added some more comments to explain the code a bit more.

Works a charm and now I can use it like this:


if ( $this->get_current_post_type() == 'page' ) {

6 comments
  Livefyre
  • Get Livefyre
  • FAQ
FranMauri
FranMauri

For some reason that functions returns NULL to me ;(

fredbradley
fredbradley

Can't you just use the global $post_type ?

DomenicFiore
DomenicFiore

I added this elseif to get it working on an edit page:


elseif (get_post_type($_REQUEST['post']))
        return get_post_type($_REQUEST['post']);

sixtyseven
sixtyseven

@DomenicFiore Perfect, exactly what was missing!


Sandro Paganotti
Sandro Paganotti

Thanks ! I'm working on a plugin too and this comes really handy!

Nicolas123
Nicolas123

I was using something like var post_type = jQuery("form#post #post_type").val(); if(post_type == "page"){...}. get_current_post_type() will be really usefull to get ride on my JS patch.