1

my plugin has a save_post hook used to save the custom fields. now I add a publish_slider hook used to change a checkbox's status(slider is a custom post type). but it doesn't work properly.

the save_post hook is as below

add_action('save_post', 'save_post_func', 10, 1);
function save_post_func($post_id) {
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    return;
  if ( isset($_POST['action']) && $_POST['action'] == 'inline-save' )
    return;
  ...
  if(isset($_POST[$key])){
    update_post_meta($post_id,$key,$_POST[$key]);
  }
  else{
    if($field['type']=='checkbox'){
    update_post_meta($post_id,$key,0);
  }
}

this would set a default 0 to the checkbox when I save a new post and didn't set the checkbox, this is no problem.

and the publish_slider hook is as below

add_action('publish_slider', 'publish_slider_func', 10, 1);
function publish_slider_func($post_id) {
  ...
  update_post_meta($post_id, 'closeSlider', '1');
}

I hope when I publish the slider post, the closeSlider checkbox would be set to 1 automatically, but it wouldn't.

if I delete the update_post_meta($post_id,$key,0); in the first function, then the publish hook works

  • The publish_posttype action runs before the save_post action, so anything that happens in save_post will overwrite what you do in publish_posttype – czerspalace Jul 8 '15 at 17:58
  • @czerspalace thanks for your reply. so how about other {status}_{post_type} hooks? do they all run before save_post? – Brick Yang Jul 9 '15 at 0:52
  • Yes, if you look here core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/… {new_status}_{post_type} runs in wp_transition_post_status which runs before save_post – czerspalace Jul 9 '15 at 6:11

Your Answer

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

Browse other questions tagged or ask your own question.