save_post_post-type WordPress Hook,仅在我创建新帖子时有效,但在编辑帖子时不起作用
我有一个自定义帖子类型“Compania”,它有一个名为“compania_id”的元字段,我希望该字段=当前post_id
我实现了此操作来执行此操作,并且当我创建新帖子时它可以工作(compania_id 自动等于当前帖子) post_id)但是在旧的 compania 帖子中,当此代码尚未创建时,当我编辑这些帖子并保存时,compania_id 不会像我创建新帖子时那样填充 post_id,为什么?
function anadir_post_id($post_id){
update_post_meta($post_id, 'compania_id', $post_id);
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 1);
I have a Custom Post Type "Compania", it has a meta field called "compania_id" i would like that field = current post_id
I implemented this action to do that and it works when i create a new post (compania_id automatically is equal to current post_id) but in older compania post when this code hasnt been created, when i edit these post and save, compania_id isnt filled with the post_id as when i create a new post, why??
function anadir_post_id($post_id){
update_post_meta($post_id, 'compania_id', $post_id);
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的挂钩具有三个参数。因此,您的代码需要如下所示。
如果您使用旧版本的 php,它将如下所示:
当然,如果您有权访问帖子的
wp_postmeta
行,您就已经知道它的ID
。您在这里存储的内容可能是多余的。The hook you use has three parameters. Therefore your code needs to look like this.
If you have an older version of php it will look like this:
And, of course, if you have access to a post's
wp_postmeta
rows, you already know itsID
. What you store here may be redundant.