Codex

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

Plugin API/Action Reference/post updated

This page redirects to an external site: https://developer.wordpress.org/reference/hooks/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:

<code style="color: #000000">
<span style="color: #0000BB"><?php
</span><span style="color: #007700">function </span><span style="color: #0000BB">check_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$post_ID</span><span style="color: #007700">, </span><span style="color: #0000BB">$post_after</span><span style="color: #007700">, </span><span style="color: #0000BB">$post_before</span><span style="color: #007700">){
    echo </span><span style="color: #DD0000">'Post ID:'</span><span style="color: #007700">;
    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$post_ID</span><span style="color: #007700">);

    echo </span><span style="color: #DD0000">'Post Object AFTER update:'</span><span style="color: #007700">;
    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$post_after</span><span style="color: #007700">);

    echo </span><span style="color: #DD0000">'Post Object BEFORE update:'</span><span style="color: #007700">;
    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$post_before</span><span style="color: #007700">);
}

</span><span style="color: #0000BB">add_action</span><span style="color: #007700">( </span><span style="color: #DD0000">'post_updated'</span><span style="color: #007700">, </span><span style="color: #DD0000">'check_values'</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #0000BB">3 </span><span style="color: #007700">); </span><span style="color: #FF8000">//don't forget the last argument to allow all three arguments of the function
</span><span style="color: #0000BB">?>
</span></code>

Result

The Result then would be this:

<code style="color: #000000">
<< 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)
</code>

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

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