WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/post updated

Use this hook whenever you need to compare values before and after the post update.

This hook runs after the database update.

This hook pass up to 3 arguments, as follows:

  • $post_ID ;
  • $post_after (post object after the update);
  • $post_before (post object before the update);

Example

Suppose we have a post named Original Title and we edit it to Edited Title.

Let's hook to post_updated to check what has changed:
<?php
function check_values($post_ID$post_after$post_before){
    echo 
'Post ID:';
    
var_dump($post_ID);

    echo 
'Post Object AFTER update:';
    
var_dump($post_after);

    echo 
'Post Object BEFORE update:';
    
var_dump($post_before);
}

add_action'post_updated''check_values'10); //don't forget the last argument to allow all three arguments of the function
?>

Result

The Result then would be this:
<< Post ID: >>
int 1403
<< Post Object AFTER update: >>
object(WP_Post)[7722]
  public 'ID' => int 1403
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2014-08-10 18:19:43' (length=19)
  public 'post_date_gmt' => string '2014-08-10 18:19:43' (length=19)
  public 'post_content' => string  (length=0)
  public 'post_title' => string 'Edited Title' (length=12)
  public 'post_excerpt' => string 
 (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'closed' (length=6)
  public 'ping_status' => string 'closed' (length=6)
  public 'post_password' => string  (length=0)
  public 'post_name' => string 'edited-title' (length=12)
  public 'to_ping' => string 
 (length=0)
  public 'pinged' => string  (length=0)
  public 'post_modified' => string '2014-08-10 19:41:46' (length=19)
  public 'post_modified_gmt' => string '2014-08-10 19:41:46' (length=19)
  public 'post_content_filtered' => string 
 (length=0)
  public 'post_parent' => int 0
  public 'guid' => string 'http://localhost:8888/mysite/?post_type=test_post&#038;p=1403' (length=67)
  public 'menu_order' => int 0
  public 'post_type' => string 'procedimentos' (length=13)
  public 'post_mime_type' => string  (length=0)
  public 'comment_count' => string '0' (length=1)
  public 'filter' => string 'raw' (length=3)
<< Post Object BEFORE update: >>
object(WP_Post)[7724]
  public 'ID' => int 1403
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2014-08-10 18:19:43' (length=19)
  public 'post_date_gmt' => string '2014-08-10 18:19:43' (length=19)
  public 'post_content' => string 
 (length=0)
  public 'post_title' => string 'Original Title' (length=14)
  public 'post_excerpt' => string  (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'closed' (length=6)
  public 'ping_status' => string 'closed' (length=6)
  public 'post_password' => string 
 (length=0)
  public 'post_name' => string 'original-title' (length=14)
  public 'to_ping' => string  (length=0)
  public 'pinged' => string 
 (length=0)
  public 'post_modified' => string '2014-08-10 19:41:14' (length=19)
  public 'post_modified_gmt' => string '2014-08-10 19:41:14' (length=19)
  public 'post_content_filtered' => string  (length=0)
  public 'post_parent' => int 0
  public 'guid' => string 'http://localhost:8888/mysite/?post_type=test_post&#038;p=1403' (length=67)
  public 'menu_order' => int 0
  public 'post_type' => string 'procedimentos' (length=13)
  public 'post_mime_type' => string 
 (length=0)
  public 'comment_count' => string '0' (length=1)
  public 'filter' => string 'raw' (length=3)


With both values you can easily compare between the two moments.

This page is marked as incomplete. You can help Codex by expanding it.