Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have three different posts types registered:

register_post_type( 'foo' , $args );
register_post_type( 'bar' , $args );
register_post_type( 'baz' , $args );

When I am creating a new, say, "foo" post, I only want to save the meta box for "foo", what do I do?

add_action('save_post', 'save_details');

So, if my post_type were called "foo_post":

add_meta_box( 
    $id, 
    $title, 
    $callback, 
    // SPECIFY THE POST TYPE HERE!!!
    "foo_post", 
    $context, 
    $priority, 
    $callback_args );

What do I do to save only "foo post" meta box when I am creating / edit a "foo post" page?

share|improve this question
    
If you specify the post type in your add_meta_box then it should only add the meta box for that post type, and if there's no meta box no data will be sent... or have I misunderstood...? :S. In your example 'foo_post' should be just 'foo'. –  Stephen Harris Feb 23 '12 at 23:16
add comment

2 Answers

up vote 2 down vote accepted

Since save_post gives you the post_id of the current post, you simply want to check what post_type it is by using get_post_type($post_id)

like:

add_action('save_post', 'save_details')

function save_details($post_id)
{
     $post_type = $_REQUEST['post_type'];

     if ('foo_post' == $post_type)
     {
          // save stuff for foo_post
     }
     elseif ('bar_post' == $post_type)
     {
          // save stuff for bar_post
     }
}

and so on...

get_post_type() codex page

share|improve this answer
    
You should just grab the post type from the $_REQUEST rather than add another DB call. :) –  Brian Fegter Feb 23 '12 at 23:08
    
:) thanks for the edit. –  ungestaltbar Feb 23 '12 at 23:16
1  
But custom meta boxes are only added on a per-post-type basis, since the add_meta_box() call only accepts one post type. (Unless multiple add_meta_box() calls are made, to add the same meta box to different post types.) Also: you should simply be able to globalize $post, and use $post->post_type here, which I think is inherently safer than using $_REQUEST data. –  Chip Bennett Feb 24 '12 at 0:12
    
@ChipBennett I agree using the global $post object for adding meta boxes and the content within the callbacks. However, the $_REQUEST for save_post action gives you what type of post is being saved. Wouldn't it be more efficient to use the supplied $_POST data that's being saved? I think there may be two different subjects going on here. :) –  Brian Fegter Feb 24 '12 at 3:18
    
I'm not sure the relative efficiency is more than a trivial difference. Both the $_POST data and the cached $post global are available at this point in the processing. In such a scenario, my preference is to use $post->post_type, but it may be nothing more than a matter of preference. –  Chip Bennett Feb 24 '12 at 3:45
show 1 more comment

I assume you're talking about a custom post meta box? If so, then you need to ensure that you *specify the $posttype when you call add_meta_box(). e.g.:

<?php
add_meta_box( 
    $id, 
    $title, 
    $callback, 
    // SPECIFY THE POST TYPE HERE!!!
    $post_type, 
    $context, 
    $priority, 
    $callback_args );
?>

If you're not referring to a custom post meta box, then you'll need to provide more information, regarding what post meta box you're trying to manipulate.

share|improve this answer
    
Yes, that's what I mean. I added some more detail to my question. What do I put in my callback, "save_details" so that I am only saving this data for my "foo post" custom post type? Does wordress just know to save for that custom post type only? –  redconservatory Feb 23 '12 at 21:16
    
Can you post your actual add_meta_box() code in your question? –  Chip Bennett Feb 23 '12 at 22:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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