【发布时间】:2021-07-06 20:52:30
【问题描述】:
每次发布帖子时,我都会发送推送通知。一切都按预期工作,但 ACF 字段为空。相反,当我重新保存帖子数据时,数据正确。
基本工作流程
- 用户发布帖子
- 触发操作发送推送通知
- ACF 字段的有效负载为空(标题、摘录、其余字段都可以)
我已经阅读了几个线程,但是在创建帖子后无法实现将 ACF 字段传递到休息。
我尝试过的:
优先级为 100,以确保帖子已保存,然后再获取信息。
add_action(
"save_post",
["YaguaretePluginApiBlog", "postPushNotification"],
100,
3
);
尝试使用transition_post_status,结果相同。
add_action(
"transition_post_status",
["YaguaretePluginApiBlog", "postPushNotification"],
99,
3
);
或
add_action('rest_after_insert_post', ["YaguaretePluginApiBlog", "postPushNotification"], 100, 3);
所有这些都可以毫无问题地触发,但在获取 ACF 字段时却没有任何作用。
也尝试了默认的 acf/save_post,但不幸的是,与其他操作相比,这个甚至没有启动
add_action("acf/save_post", [
"YaguaretePluginApiBlog",
"postPushNotification",
]);
这是我试图获取 acf 字段的内容
尝试使用 ACF 中的 the_field 函数
$data[notificationType] = the_field(
"yaguarete_push_notification",
$_post->ID
);
尝试使用 post_meta 检索字段信息
$data[notificationType] = get_post_meta($post["id"])[
"yaguarete_push_notification"
][0]
尝试使用 ACF 中的 get_field
get_field("yaguarete_push_notification", $post["id"], false)
似乎没有任何效果,有经验的人可以指出我做错了什么吗?
提前致谢
【问题讨论】:
-
你检查过 acf/save_post 钩子吗? advancedcustomfields.com/resources/acf-save_post 我猜在默认保存后功能期间 ACF 数据尚未更新,因此您描述的尝试失败。
-
嗨@mynd,感谢您的回复,是的,我确实尝试过,但由于某种原因没有触发。用它更新帖子
标签: php rest action advanced-custom-fields