php - 函数 Hook add_post_meta ACF Post 对象
问题描述
我在functions.php中有一个函数:
function create_whiteboard( $form_id, $post_id, $form_settings ) {
$current_user = wp_get_current_user();
$post_id = wp_insert_post(array (
'post_type' => 'whiteboard',
'post_title' => 'Whiteboard for ' . $current_user->user_firstname . ' ' . $current_user->user_lastname,
'post_status' => 'publish',
));
add_post_meta($post_id, 'project_select', $post_id, true);
}
add_action('create_whiteboard_hook', 'create_whiteboard', 10, 3 );
这样做的原因是它在白板帖子类型中创建了一个帖子 - 但它不会更新我的帖子对象字段 (project_select)。如果我指定一个 ID:
add_post_meta($post_id, 'project_select', '1', true);
然后它确实有效 - 我的问题是如何将刚刚创建的帖子的 ID 传递给这个?
解决方案
被从的$post_id
返回值赋值覆盖wp_insert_post
。
照原样,创建的白板帖子是用元数据装饰的,而不是预期的帖子。
您可以通过为引用从调用返回值的变量使用不同的名称来解决此问题wp_insert_part
。
function create_whiteboard( $form_id, $post_id, $form_settings ) {
$current_user = wp_get_current_user();
$whiteboard_post_id = wp_insert_post(array (
'post_type' => 'whiteboard',
'post_title' => "Whiteboard for {$current_user->user_firstname} {$current_user->user_lastname",
'post_status' => 'publish',
));
add_post_meta($post_id, 'project_select', $whiteboard_post_id, true);
}
推荐阅读
- anylogic - 如何通过 Java 将数据条添加到图表中?
- php - 必须配置路径“enqueue_elastica”处的子节点“transport”
- bash - 如何使用密码在非交互式 bash 脚本中 ssh
- java - Outlook 不会预览由 ical4j 创建的 ics 文件
- excel - 如果有超过 360 行数据,VBA 代码会提前离开循环
- java - 如何在java中只打印时间?
- c# - 如果有人将字符串输入双精度并且在发送错误消息后,他们如何再次发送错误
- java - 为什么不能用 sysem.out.print 命令打印 2 个字符
- angular - Angular Azure AD - MsalModule。认证后获取Access Token
- javascript - Momentjs如何获取时区时间 - 不基于计算机日期时间