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.

Is it possible within the save_post action to determine whether it's a new post being created or an existing post being update?

share|improve this question
    
I don't think this is possible. See my comment below @moraleida's answer. Why do you need to know if it's a new post or being updated? There may be a work-around or an alternative approach. –  Stephen Harris Apr 12 '12 at 18:28

3 Answers 3

up vote 4 down vote accepted

I ended up just checking for the existence of a custom value prior to setting it. That way, if it's a newly created post the custom value would not yet exist.

function attributes_save_postdata($post_id) {
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
  if (!wp_verify_nonce($_POST['_attributes_noncename'], plugin_basename(__FILE__))) return;
  if ('page' == $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id)) return;
  } else {
    if (!current_user_can('edit_post', $post_id)) return;
  }
  $termid = get_post_meta($post_id, '_termid', true);
  if ($termid != '') {
    // it's a new record
    $termid = 'update';
  } else {
    // it's an existing record
  }
  update_post_meta($post_id, '_termid', $termid);
}
add_action('save_post', 'attributes_save_postdata');
share|improve this answer
    
For this to work, do you have to first create the custom field using add_post_meta? –  MF1 Apr 9 '13 at 2:03
    
Per the Codex: [update_post_meta] may be used in place of add_post_meta() function. codex.wordpress.org/Function_Reference/update_post_meta –  hereswhatidid Jan 20 at 21:46

When save_post is triggered, all information about that post is already available, so in theory you could use

function f4553265_check_post() {

    if (!get_posts($post_id)) {
    // if this is a new post get_posts($post_id) should return null
    } else {
    // $post_id already exists on the database
    }
}
add_action('save_post','f4553265_check_post');

this is untested, though. =)

share|improve this answer
1  
By the time you get to save_post the post itself would have already been saved to database - so get_posts would return the current post. –  Stephen Harris Apr 12 '12 at 18:25
    
True, just checked it in the Codex. Thanks for the heads-up. –  moraleida Apr 12 '12 at 18:59

You can use pre_post_update action hook for the update code and save_post for the new post code. It works before a post is updated.

share|improve this answer
2  
save_post hook is fired both when a post is created and updated (after WordPress has saved it to the database). pre_post_update is fired when a post is updated, but prior to the post being updated - this can be important. –  Stephen Harris Aug 12 '12 at 18:59

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.