21

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

  • 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
16

Since WordPress version 3.7. - IIRC - the save_post hook - more information about the hook and its usage at Code Reference: save_post and Codex: save_post - has a third parameter $update which can be used to determine just that.

@param     int               $post_ID     Post ID.
@param     WP_Post     $post          Post object.
@param     bool            $update     Whether this is an existing post being updated or not.


Note:

$update is not always true – you can see and test it yourself with below code. It is not well documented though, possibly far from optimally named, and hence creates misleading expectations. Below code can be used for some debugging, play around with when to intercept code execution, because otherwise you won't see the information/messages. I think, the culprit in deceptive behavior is the handling of revisions and auto saves – which could be disabled, but I don't recommend it, and haven't tested it. Not sure if this warrants a Trac Ticket, so I didn't open one, if you think so, please follow the link and do it yourself. Aside from that, as stated in the comments, if you have a specific problem, post a new question.

add_action( 'save_post', 'debug_save_post_update', 10, 3 );
function debug_save_post_update( $ID, $post, $update ) {

  echo '<pre>';
  print_r( $post ); echo '<br>';
  echo '$update == ';
  echo $update ? 'true' : 'false';

  //conditions
  if( ! $update && $post->post_status == "auto-draft" ) {
    // applies to new post
    echo ' && $post->post_status == "auto-draft"';
    //die();
  } else if ( ! $update ) {
    // applies basically to the (auto saved) revision 
    //die();
  } else {
    // applies to updating a published post
    // when there is a revision, which is normally the case, 
    // standard behavior of WordPress, then it is considered 
    // an update, which is where the confusion sets in
    // there are other methods, like checking time or post status
    // depending on your use case it might be more appropriate 
    // to use one of those alternatives 
    //die();
  }

  echo '</pre>';
  //die();
}
  • 3
    The $update parameter is ALWAYS true even when it is a new post. So this parameter is useless. Not sure if it ever worked at all, but it sure as hell isn't working the way it's documented in the latest version of wordpress 4.8. – Solomon Closson Jul 20 '17 at 20:32
  • @SolomonClosson If you take a look at wp_publish_post, then yes. But that isn't true for its usage in wp_insert_post. I've written a debug function, I add it to the answer. – Nicolai Jul 21 '17 at 0:11
  • @SolomonClosson If you have a actual concrete problem, please ask a new question. Take a look at the revisions for the debug function an explanation. – Nicolai Jul 21 '17 at 0:34
  • The save_post hook has a 3rd parameter that is always set to TRUE, so not sure what this has to do with other hooks, not speaking about other hooks. I'm talking about the hook in your answer. This is incorrect. – Solomon Closson Jul 23 '17 at 23:42
  • @SolomonClosson As I said the hook is occurring two times: wp_insert_post(), wp_publish_post(). The latter is only future posts, there $update is set to be always true. Otherwise, in regards to wp_insert_post(), $update is not always true. – Nicolai Jul 24 '17 at 13:08
11

The way I perform this check (within a hooked function) is to compare the post date and modified date (in GMT for standardisation)

function check_new_vs_update( $post_id ){
    $myPost        = get_post($post_id);
    $post_created  = new DateTime( $myPost->post_date_gmt );
    $post_modified = new DateTime( $myPost->post_modified_gmt );

    if( abs( $post_created->diff( $post_modified )->s ) <= 1 ){
        // New post
    }else{
        // Updated post
    }
}
add_action('save_post', 'check_new_vs_update' );

This works because even at creation the post has a 'modified' date attached to it, which is exactly the same as the 'created' date, but we allow a variance of 1 second either way in case a second ticks over during the creation of the post.

  • 1
    Sometimes the post_date_gmt is 2019-03-12 01:31:30 and the post_modified_gmt is 2019-03-12 01:31:31. :( – He Yifei 何一非 Mar 12 at 4:06
  • 1
    @HeYifei何一非 good point, if the processing begins at the end of a given second, this could happen. I've updated my answer, thanks – James Cushing Mar 12 at 13:04
  • Guys, just an info. The hook is fired on restoring and deleting a post. – melvin May 9 at 11:39
6

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');
  • 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 '14 at 21:46
  • This might fail, if posts were created before the code hook gets enabled via a plugin activation. Older posts dont have the meta set, hence the first update for them will be considered as new. – Vasu Chawla May 5 '18 at 19:13
4

Example to ialocin answer with "update" paremeter:

function save_func($ID, $post,$update) {

   if($update == false) {
     // do something if its first time publish
   } else {
     // Do something if its update
   }
}

add_action( 'save_post', 'save_func', 10, 3 );
  • 2
    A better way to structure this would be either putting the update block first, allowing to just do if($update) or keeping the new block first but using if( ! $update ). The latter will get OP into better practice and is preferred over your method by WordPress coding standards in cases like the ternary operator – James Cushing Nov 17 '16 at 11:38
1

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.

  • 4
    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
1

As Darshan Thanki hinted (and Stephen Harris further elaborated), you can use pre_post_update to your advantage.

global $___new_post;
$___new_post = true;

add_action(
  'pre_post_update',
  function() {
    global $___new_post;
    $___new_post = false;
  },
  0
);

function is_new_post() {
  global $___new_post;
  return $___new_post;
}

The reason why I used globals is because function is_new_post() use ( &$new_post ) is not valid in PHP (shocking...) so pulling in that variable into the function scope doesn't work -- hence the global.

Note that this can really only reliably be used within/after the save_post event (which is usually sufficient, at least for what we're doing with it).

0

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. =)

  • 3
    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
0

Another approach that uses a built-in function and no addition to the database would involve get_post_status().

$post_status = get_post_status();
if ( $post_status != 'draft' ) {
    //draft
} else { 
    //not a draft: can be published, pending, etc. 
}

Note however that it might not be appropriate if you plan to later set the status back to "draft" – your instructions would be repeated the next time you will update the post. Depending on the context, you might want to consider the various strings that can be returned by get_post_status() to build a more appropriate scenario.

See Codex for get_post_status() and Post Status

Possible values are:

  • 'publish' - A published post or page
  • 'pending' - post is pending review
  • 'draft' - a post in draft status
  • 'auto-draft' - a newly created post, with no content
  • 'future' - a post to publish in the future
  • 'private' - not visible to users who are not logged in
  • 'inherit' - a revision. see get_children.
  • 'trash' - post is in trashbin. added with Version 2.9.
  • I don't think this does what was asked for. If I create a new post and then hit 'Publish', save_post() is executed for the first time, but during that execution get_post_status() already returns 'publish' and not 'draft', even though it is only in the process of being published. – cgogolin Nov 25 '17 at 18:39

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.