WordPress 帖子保存/编辑帖子挂钩
我正在寻找一个管理挂钩,用于保存帖子后被解雇的帖子。问题:save_post
不包含已更改的 post 对象数据。新的更改只能在 $_POST
数组中找到。但我需要一种方法来在 post_name
更改后将永久链接更新为外部 API。但它不会工作,因为 $post
对象仍然是保存操作之前的旧对象。
I'm looking for a admin hook for posts that gets fired after the post has been saved. The problem: the save_post
does not contain the already changed data to the post object. The new changes can only be found in the $_POST
array. But I need a way to update the permalink to a external API once the post_name
changes. But it will not work since the $post
object is still the old one before the save action.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用优先级参数(在本例中设置为 20)更新帖子后,您应该能够挂接:
You should be able to hook in after the post has been updated using the priority argument (set to 20 in this example):
我认为最合适的方法是从数据库中查询旧值并将这些值与
$_POST
数组值进行比较。这是可以帮助您从数据库读取值的链接。
http://codex.wordpress.org/wpdb#query_-_Run_Any_Query_on_the_Database
PS:你应该当然,在将新值保存到数据库“之前”进行此比较。
I think the most suitable method is to query the old values from the database and compare the values with
$_POST
array values.Here is the link which should help you to read values from database.
http://codex.wordpress.org/wpdb#query_-_Run_Any_Query_on_the_Database
P.S: You should of course make this comparison "before" saving the new values to the database.
从 WordPress 3.0.0 开始,
post_updated
挂钩可用。它有助于了解更新后帖子中发生了什么变化。您可以使用 WP Codex 中的示例作为示例。如果刚刚插入帖子,您可以使用
save_post
或save_post_{$post->post_type}
挂钩。检查第三个参数的值以确保该帖子是新的。Since WordPress 3.0.0 the
post_updated
hook is available. It helps to know what has changed in the post after the update. You can use the example in the WP Codex as a sample.And if a post has just been inserted you can use
save_post
orsave_post_{$post->post_type}
hooks. Check the value of the third argument to make sure the post is new.