post_updated
云策文档标注
概述
post_updated 是一个 WordPress 钩子,在现有文章更新后触发,用于比较更新前后的文章数据。它传递三个参数:文章 ID、更新后的文章对象和更新前的文章对象。
关键要点
- 触发时机:在文章更新后、数据库更新完成后触发。
- 参数:$post_id(文章 ID)、$post_after(更新后的 WP_Post 对象)、$post_before(更新前的 WP_Post 对象)。
- 用途:适用于需要对比文章更新前后值的场景,例如跟踪标题或状态变化。
- 限制:不包含文章元数据(meta fields)的比较,仅涉及文章核心数据。
- 版本:自 WordPress 3.0.0 引入。
代码示例
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, 3 );注意事项
- 使用 add_action 时,需设置优先级为 10 并指定参数数量为 3,以确保所有参数正确传递。
- 此钩子不处理文章元数据的变更,如需比较元数据,应结合其他钩子如 updated_post_meta。
原文内容
Fires once an existing post has been updated.
Parameters
Source
do_action( 'post_updated', $post_id, $post_after, $post_before );
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 3 content
Tunn
This hook does not compare values of post meta keys if you have changed those as well. $post_after and $post_before will include strictly the new/old post data without meta fields of post.
Skip to note 4 content
Steven Lin
Example migrated from Codex:
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:
Let’s hook to
post_updatedto check what has changed: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); } add_action( 'post_updated', 'check_values', 10, 3 ); //don't forget the last argument to allow all three arguments of the function ?>The Result then would be this:
<br />
<b>Post ID:</b><br />
int 1403<br />
<b>Post Object AFTER update:</b><br />
object(WP_Post)[7722]<br />
public 'ID' => int 1403<br />
public 'post_author' => string '1' (length=1)<br />
public 'post_date' => string '2014-08-10 18:19:43' (length=19)<br />
public 'post_date_gmt' => string '2014-08-10 18:19:43' (length=19)<br />
public 'post_content' => string (length=0)<br />
public 'post_title' => string 'Edited Title' (length=12)<br />
public 'post_excerpt' => string (length=0)<br />
public 'post_status' => string 'publish' (length=7)<br />
public 'comment_status' => string 'closed' (length=6)<br />
public 'ping_status' => string 'closed' (length=6)<br />
public 'post_password' => string (length=0)<br />
public 'post_name' => string 'edited-title' (length=12)<br />
public 'to_ping' => string (length=0)<br />
public 'pinged' => string (length=0)<br />
public 'post_modified' => string '2014-08-10 19:41:46' (length=19)<br />
public 'post_modified_gmt' => string '2014-08-10 19:41:46' (length=19)<br />
public 'post_content_filtered' => string (length=0)<br />
public 'post_parent' => int 0<br />
public 'guid' => string 'http://localhost:8888/mysite/?post_type=test_post&p;=1403' (length=67)<br />
public 'menu_order' => int 0<br />
public 'post_type' => string 'procedimentos' (length=13)<br />
public 'post_mime_type' => string (length=0)<br />
public 'comment_count' => string '0' (length=1)<br />
public 'filter' => string 'raw' (length=3)<br />
<b>Post Object BEFORE update:</b><br />
object(WP_Post)[7724]<br />
public 'ID' => int 1403<br />
public 'post_author' => string '1' (length=1)<br />
public 'post_date' => string '2014-08-10 18:19:43' (length=19)<br />
public 'post_date_gmt' => string '2014-08-10 18:19:43' (length=19)<br />
public 'post_content' => string (length=0)<br />
public 'post_title' => string 'Original Title' (length=14)<br />
public 'post_excerpt' => string (length=0)<br />
public 'post_status' => string 'publish' (length=7)<br />
public 'comment_status' => string 'closed' (length=6)<br />
public 'ping_status' => string 'closed' (length=6)<br />
public 'post_password' => string (length=0)<br />
public 'post_name' => string 'original-title' (length=14)<br />
public 'to_ping' => string (length=0)<br />
public 'pinged' => string (length=0)<br />
public 'post_modified' => string '2014-08-10 19:41:14' (length=19)<br />
public 'post_modified_gmt' => string '2014-08-10 19:41:14' (length=19)<br />
public 'post_content_filtered' => string (length=0)<br />
public 'post_parent' => int 0<br />
public 'guid' => string 'http://localhost:8888/mysite/?post_type=test_post&p;=1403' (length=67)<br />
public 'menu_order' => int 0<br />
public 'post_type' => string 'procedimentos' (length=13)<br />
public 'post_mime_type' => string (length=0)<br />
public 'comment_count' => string '0' (length=1)<br />
public 'filter' => string 'raw' (length=3)<br />