函数Hook add_post_meta ACF Post Object

时间:2018-05-18 02:52:44

标签: php wordpress advanced-custom-fields

我在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传递给它?

1 个答案:

答案 0 :(得分:0)

来自$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);
}