WordPress:在更新帖子之前获取自定义字段值
我想做的事情: 在 WordPress 中,当我编辑帖子的自定义字段值时,获取旧值和新值,然后比较它们。
我现在得到的: 我只能得到新的值。我挂钩 pre_post_update
操作,并使用 get_post_meta()
(以及 http://codex.wordpress.org/Custom_Fields) 获取值。
我也尝试过get_post()
,它可以返回post_content
、post_title
等的旧值。但是get_post()
code> 不包含自定义字段值。
我认为当 pre_post_update
钩子触发时,这些值还没有保存在数据库中,不是吗?我可以获得帖子标题的旧值等事实证明了这一点。那么必须有某种方法来获取自定义字段的旧值。
如果您能指出其他比较自定义字段的旧值和新值的方法,我们也很欢迎。
非常感谢。
What I want to do:
In WordPress, when I edit the custom field value of a post, get the old value and the new value, and compare them.
What I get now:
I can only get the new value. I hook to the pre_post_update
action, and I use get_post_meta()
(and a bunch of related functions described in http://codex.wordpress.org/Custom_Fields) to get the value.
I also tried get_post()
, which can return the old value of post_content
, post_title
, etc. But get_post()
does not include the custom field values.
I think that at the time when the pre_post_update
hook fires, the values have not been saved in the database yet, isn't it? The fact that I can get old value of post title, etc. proves it. Then there must be some way to get the old value of custom fields.
It is also welcomed if you can point out other ways to compare the old value and new value of custom fields.
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Pigi已经表明了这一点。它将被覆盖。为什么不使用带有两个键(旧键和新键)的自定义字段,并将旧值分配给旧键,将新值分配给新键。
Pigi has made the point. it will be over written. why not use too custom fields with two keys, old Key and new Key, and assign both old value to old key,new value to new key.
您无法将旧的自定义字段与新的自定义字段进行比较,因为当您更新它们时,它也会在数据库中的“wp_post_meta”表中重写。你会因为新的而失去旧的价值。
You can't compare an old custom field with new, because when you update them it will rewritten also in the "wp_post_meta" table in db. You lose the old value for the new.