Get old values for post before saving new ones

I’m using the save_post hook to do some additional logic after a post has been saved.

However I need to find a way of getting the old values of the post, specifically in my case the slug/handle aka post_name.

Tried using the wp_insert_post_data filter to catch the post and add the old slug as an additional field pre save but that doesn’t seem to work.

TL;DR want to achieve something like this:

public function post_sync( $post_id, $post, $update ) {

$post_new_handle = $post->post_name;
$post_old_handle = $post->post_old_name;

if($post_new_handle !== $post_old_handle) {
    //additional logic
}

    //additional logic

}

Any way of achieving this?
Thanks.

1
1

The post_updated action gives you both old and new values as arguments before the new values are saved:

do_action( 'post_updated', $post_ID, $post_after, $post_before );

Leave a Comment