I am trying to get the $post_before
and $post_after
objects for comparison when updating a post in WordPress.
However, when I submit the update, the $post_before
and $post_after
objects are identical and show the updated post object.
add_action('post_updated', [$this, 'comparePosts'], 10, 3);
function comparePosts($post_ID, $post_after, $post_before)
{
echo '<b>Post ID:</b><br />';
var_dump($post_ID);
echo '<b>Post Object AFTER update:</b><br />';
var_dump($post_after->post_name);
echo '<b>Post Object BEFORE update:</b><br />';
var_dump($post_before->post_name);
}
After researching, I have found two related bug tickets https://core.trac.wordpress.org/ticket/47908 and https://core.trac.wordpress.org/ticket/45114.
The Bug is related to the Gutenberg editor's order of processing the update hooks. The ticket has been closed, but even in the latest version of WordPress [5.9.2], the issue still exists.
I have also tried to use different hooks including pre_post_update
, post_updated
, save_post
but all of them carry the updated post object in $post_before
and $post_after
. How can I get the real $post_before
and $post_after
objects.