How can I get the post_type while on the post.php page?

I'm working on a plugin that lets users create a new post type. Then in the new post type, a meta box will be added with some options. The meta box to be added is based on certain parameters that were set while creating the new post type.

So I need to check what the post_type is, and if it's a certain post_type, a certain meta box will be added to the post edit page.

I have it working just fine for add new page (post-new.php) using:

$post_type = $_GET['post_type'];

But this doesn't seem to get the post type on the post.php page when editing an existing post within the newly created post type.

I've also tried:

$post_type = get_post_type( $post->ID );


$post_type = $post->post_type;

Any ideas?


Example code from comment below:

$post_type = get_post_type($post->ID); 
$custom_query = new WP_Query(array( 'post_type' => 'custom-types', 'name' => ''.$post_type.'', 'posts_per_page' => 1 )); 
while ($custom_query->have_posts()){ 
    global $post; 
    $title = get_the_title(); 
    $option_a = get_post_meta($post->ID, '_option_a', true); 
    $option_b = get_post_meta($post->ID, '_option_b', true); 


This code works on the post-new.php file:

But not on the post.php file. However, when I fill in the name parameter manually, it works on the post.php file.

The get_post_type($post->ID); is being ignored in the meta boxes on the post.php page, so instead of pulling data from the post with the name of the post type, it's just pulling data from the most recent post, as if I ran the query without the name parameter at all.


When I use global $post; no data is pulled from the WP_Query on the post.php page.

With no global $post; the most recent post is pulled from the WP_Query.

When I manually enter the name parameter, it works fine on the post.php page.

The code, as is, works fine on the post-new.php page.

I tried browsing through the source code of the post.php page to see how post_type is referenced there, but no luck. . .figuring out what to use to get the post type.

  • The example code looks like template code. Can you post the admin code - i.e. the code you're using on the post.php page? – Chip Bennett Jan 26 '12 at 23:35
  • I'm not actually modifying the post.php file. The meta boxes are getting added just fine if I manually type the post type name where i have: 'name' => ''.$post_type.'', But I need the post_type name to be grabbed dynamically. When I click "Add New" the WP_Query is pulling data from the custom-types post with the same name as the post type, as it should. . . But when I save the data, and click "edit" the WP_Query is pulling data from the most recently created post type, ignoring the $post_type parameter. Does that make sense? – Jason Bahl Jan 26 '12 at 23:49

Why can't you just globalize $post and then get the post type? e.g.

global $post;
$post_type = get_post_type( $post->ID );

In what context are you placing this code? In a metabox callback, or what?

(Also: why are you using $_GET data?)

  • 1
    As briefly described above, I'm making a plugin that allows the user to create a new custom post type on the fly. As they create the new post type, they can select certain options, and based on those options, a different meta box will be applied to the new post type they created. It's working perfectly when I click "Add New" (created post type) but not when I save that new post. . . – Jason Bahl Jan 26 '12 at 23:11
  • Here's how I'm doing it. . . I have a custom post type set up with various options. Say a user creates one titled Custom 1, and selects option A. A new post type called Custom 1 is created and the Option A meta box is attached. Then if they create another one called Custom 2 and select Option B, a new post type titled Custom 2 is created with the Option B meta box attached. To check what option was selected, I'm getting the post type of the current post, then running a loop using the post type as the name, because the post_type_name is the same as the name of the post that created it. – Jason Bahl Jan 26 '12 at 23:13
  • Describing how you're doing it is no substitute for seeing your actual code. – Chip Bennett Jan 26 '12 at 23:16
  • Let me post some code – Jason Bahl Jan 26 '12 at 23:18
  • <?php $post_type = get_post_type($post->ID); $custom_query = new WP_Query(array( 'post_type' => 'custom-types', 'name' => ''.$post_type.'', 'posts_per_page' => 1 )); // STARTS THE LOOP while ($custom_query->have_posts()){ $custom_query->the_post(); // GETS POST META VARIABLES global $post; $title = get_the_title(); $option_a = get_post_meta($post->ID, '_option_a', true); $option_b = get_post_meta($post->ID, '_option_b', true); } ?> – Jason Bahl Jan 26 '12 at 23:22

When I changed the loop to

foreach ($custom_query as $custom) {

Instead of

while ($defaulttheme_query->have_posts()){

Everything started working.

For some reason the post type won't echo within the "while" loop but will within the "foreach" loop.

Your Answer

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

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