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.
2
Answers
Just tested this in WordPress 6.0.2(latest) and latest gutenberg plugin and it’s working as expected. Make note post name is the slug, so you must modify slug to see difference.
Also if that doesn’t work, do a comparison check first before dumping:
I see you tried a bunch of hooks but you can also try wp_after_insert_post hook which works well with gutenberg.
I can confirm this known issue is happening on WP 6.0.2 for me.
You can overcome this by using both
pre_post_update
andwp_after_insert_post
hooks within a class.