保存帖子后的 Wordpress 挂钩 [英] Wordpress hooks after a post is saved

查看:23
本文介绍了保存帖子后的 Wordpress 挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找并找到了很多不同的答案,但是我没有找到一个明确的答案.

I have been searching and found a lot of various answers, however, I have not found a definitive answer.

我需要在将帖子保存到数据库后立即运行一个函数.这包括帖子的各个方面,包括帖子元数据.我试图连接到 save_post 但这似乎在保存 post metas 之前运行我的函数.我也尝试过 post_updated 和 updated_postmeta,但我的函数似乎无法在其中任何一个上运行.

I need to run a function right after a post is done saving to the database. This includes every aspect of the post including post metas. I have tried to hook into save_post but that seems to run my function before post metas are saved. I have also tried post_updated and updated_postmeta, but my function doesn't seem to run on either of them.

另一件需要注意的事情,我需要在我的函数中访问帖子 ID.

Another thing to note, I need to have access to the post ID inside my function.

编辑我的插件使用高级自定义字段插件,我编写的函数使用 update_field 来创建新的帖子元数据或基于某些内容更新现有的元数据.此代码有效.当我在 post_updated 挂钩上运行该函数时,该函数似乎在运行,但没有任何反应.如果我将 die() 添加到我的函数的末尾,我的代码可以工作,但是 die 会杀死页面,而我只剩下 url wp-admin/post.php 上的空白页面.所以添加 die 可以让我的函数工作,我不知道为什么没有 die 就不行.

Edit, My plugin uses the Advanced Custom Fields plugin and the function I have coded uses update_field to either create new post metas or update existing one based on some stuff. This code works. When I run the function at the post_updated hook the function seems to run but nothing happens. If I add die() to the end of my function my code works, but die kills the page and all I am left with is a blank white page at the url wp-admin/post.php. So adding die allows my function to work and I am not sure why it would not work without die.

推荐答案

我能够解决我的问题.事实证明,save_post 似乎在保存后元数据后运行.我的问题实际上来自我的代码中的其他问题,我可以通过更改我处理该部分脚本的方式来解决这些问题.

I was able to fix my issue. It turns out that save_post does seem to run after post metas are saved. My problem actually came from something else inside my code that I was able to fix by changing how I handled that part of my script.

这篇关于保存帖子后的 Wordpress 挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆