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 multiple plugins with different post_type(s). When I save or publish, all of the custom_post_meta that belong to one specific post_type are also saving.

I'd like to filter by post_type so that the custom_post_meta will only save in a specific post_type.

So my question is, if you have multiple plugins:

How do you save a specific postmeta to a specific post_type?

Would you just say:

if (post_type=="my_post_type") {
  add_action('save_post','save_my_meta_box_to_postmeta');
  add_action('publish_post','save_my_meta_box_to_postmeta');
}

And how do you get the post_type value?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Do the check inside the callback. save_post will pass you both the post ID and the post object:

do_action('save_post', $post_ID, $post);

So...

function save_my_meta_box_to_postmeta($post_ID, $post) {
  if ('yourtype' === $post->post_type) {
    echo '<pre>';
      var_dump($post_ID,$post);
    echo '</pre>';
    die;
  }
}
add_action('save_post','save_my_meta_box_to_postmeta',1,2);

You may want other conditionals as well like !DOING_AJAX, for example.

share|improve this answer
    
I kept on targeting save_post, edit_post, and publish_post. Does not work. In my observation, do_action('save_post',$post_ID,$post) only seem to be applicable if $post_ID and $post are given inside the variable of wp_insert_post function. By not working, I mean $post->post_type does not seem to return any value inside save_post when used in a conditional statement. –  Franz Noel Jul 9 '13 at 22:40
    
Awesome... it finally worked! I made sure that $priority and $accepted_args inside add_action were populated. Then, everything went well. Thanks. –  Franz Noel Jul 9 '13 at 23:13
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.