1

I have a few custom post types to define various types of content and one CPT to configure which fields should get rendered. The config CPT also defines a taxonomy so I can pick and choose with which config a CPT gets rendered.
The taxonomy term is defined by the post_title of its CPT.

Adding a term to the taxonomy is no problem hooking into the save_post-hook.
My problem is with changing the name of a taxonomy or deleting it. The expected behaviour is that when I change the post_title of my taxonomy that the name of the exixtig taxonomy term gets also changed. At this time there is just a new one added.
Same with deleting the CP which defines a taxonomy.

Is there a hook that actually only fires when a post gets updated and where I can get $post_before and $post_after?
If not, how can I reliably check if a post gets updated?
The save_post-hook gets fired when creating a post for the first time as well as when updating an existing post which is a bit annoying.

1

Is there a hook that actually only fires when a post gets updated and where I can get $post_before and $post_after?

You're probably looking for the post_updated hook.

Usage

add_action( 'post_updated', 'wpse_264720_post_updated', 10, 3 );
function wpse_264720_post_updated( $post_ID, $post_after, $post_before ) {

  //* Bail if this is an autosave
  if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
  }

  //* Bail if this is a post revision
  if( wp_is_post_revision( $post_ID ) ) {
    return;
  }

  //* Bail if not the correct post types
  if( ! in_array( $post_after->post_type, [ 'post-type-1', 'post-type-2' ] ) ) {
    return;
  }

  //* If you add/update/delete posts, remove action to avoid infinite loop
  remove_action( 'post_updated', 'wpse_264720_post_updated', 10 );

  //* Do something useful after the post was updated

  //* If the post was updated, remove save_post action
  remove_action( 'save_post', 'wpse_264720_save_post', 10, 3 );
}
//* Hypothetical function hooked to save_post
add_action( 'save_post', 'wpse_264720_save_post', 10, 3 );
function wpse_264720_save_post( $post_id, $post, $update ) {
  //* Bail if post was an update
  if( $update ) {
    return;
  }
  //* Do something useful on save_post
}
3
  • I did look into post_updated. How can I prevent the initial save-function from fireing through the save_hook?
    – sebastian
    Apr 24 '17 at 14:48
  • Not sure I understand. Are you saying you have another function hooked to save_post? If so, you can use the post_updated callback to remove the save_post action. Or you can use the $update parameter in the save_post function to bail if the post is an update. Both are now shown in the answer. Apr 24 '17 at 15:16
  • Yes. I have another function in the save_post-hook to initially save the taxonomy. Now I need to update and delete the taxonomy so that the old ones are not left in the taxonomy. That is where update_post comes in. $update is useless because it is always true. I guess because of the initial draft. That is why the update_post-hook always gets fires. Even when saving an initial post.
    – sebastian
    Apr 24 '17 at 15:36

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.