Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use post_updated filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 9, 2022
5 minutes read

post_updated filter

Fires once an existing post has been updated.

do_action( 'post_updated', int $post_ID, WP_Post $post_after, WP_Post $post_before )

Description

This is action hook , its fires once an existing post has been Updated.
By using this hook , you can get diffrence between $post_before and $_post_after.
Its consists of three parameters, one is $post->ID, second is $post_before and last one is $post_after.
Try updating or adding a post. You should see some output there because this is when WordPress is calling hook function.
Then in order to check to see if there was a change compare the two titles from the two before and after post objects

function check_values($post_ID, $post_after, $post_before){
if( $post_after->post_title !== $post_before->post_title ) {
// do something
}
}
add_action( 'post_updated', 'check_values', 10, 3 );

Parameters

  • $post_ID : (int) Post ID.
  • $post_after : (WP_Post) Post object following the update.
  • $post_before : (WP_Post) Post object before the update.

Live Example

To run the hook, copy the example below.

// run the action 
do_action( 'post_updated', $post_ID, $post_after, $post_before); 
  

The following example is for adding a hook callback.

// define the post_updated callback 
function action_post_updated( $post_ID, $post_after, $post_before ) { 
    // make action magic happen here... 
}; 
         
// add the action 
add_action( 'post_updated', 'action_post_updated', 10, 3 );  

To remove a hook callback, use the example below.

// remove the action 
remove_action( 'post_updated', 'action_post_updated', 10, 3 ); 

 

<?php
function check_values($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);
     echo '<b>Post Object BEFORE update:</b><br />';
    var_dump($post_before);

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.