Support » Developing with WordPress » save_post hook $update parameter

  • In the save_post hook, how exactly does the $update parameter work?
    It “seems” unstable from time to time.

    I took a look in wp_insert_post, and $update is set to TRUE if the $postarr array specifies the ID.

    I might be mistaken, but from a user standpoint, when you start creating a post and save it for the first time, $update must be false. However, it doesn’t always seem to be the case.

    Is it because of the draft auto-save? If it is, the $update parameter is really counter intuitive.

    What would be the right way to know if save_post is running an $update or not.
    I’ve seen a comparison between $post->post_date and $post->post_modified but it doesn’t seem exactly right. There is the classic DOING_AUTOSAVE constant check, but I guess that funks the thing up.

    Shouldn’t the $update be better designed?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    User standpoint? What (end) user is using $update? 🙂

    I know what you mean. It’s essentially a flag for whether an existing post record should be updated or a new one created. Very similar (but not identical) to your observation about the ID. Yes, the auto-save facility confuses the issue. Auto-saves of published posts do not update the original post record. I see $update more as an internal variable and never make use of it myself. I’ve found all the control I need from the various hooks generated by wp_transition_post_status(). I find these preferable to “save_post”, which is often much too broad. For example, newly published posts fire “draft_to_publish” and published posts being updated fire “publish_to_publish”. $update is not available through these, there’s no need.

    Once you accept that $update is mainly an internal variable, then there is no need to “design” it better. It works fine as an internal variable, even if it doesn’t make intuitive sense. I suspect it was never truly “designed”. Initially, it probably was very clear how it worked. As WP evolved, it’s use morphed to fulfill new needs to the point the assigned “update” moniker no longer made complete sense.

    I tell the End user (my client) that a bunch of stuff gets done when he first creates a custom post_type Post. I tell him all works fine, because I tested it. When he creates the Post, he takes more time than me, and the “bunch of stuff” doesn’t get done because by the time he saves, it’s “an update”.

    The transition_post_status is a good way to go in some cases, thanks for reminding me.

    Still, I believe there should be a way to hook when the user “creates a new post”. That is a Human action that should be easily identified.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘save_post hook $update parameter’ is closed to new replies.