简体   繁体   English

函数挂钩add_post_meta ACF Post对象

[英]Functions Hook add_post_meta ACF Post Object

I have a function in functions.php: 我在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 );

This works in that it creates a post in whiteboard post type - but it doesn't update my post object field (project_select). 它的工作方式是创建白板帖子类型的帖子-但不会更新我的帖子对象字段(project_select)。 If I specify an ID: 如果我指定一个ID:

add_post_meta($post_id, 'project_select', '1', true);

Then it does work - my question is how do I pass the ID of the post just created into this? 然后它起作用了-我的问题是如何将刚刚创建的帖子的ID传递给它?

The $post_id is overwritten by the assignment of the return value from wp_insert_post . $post_idwp_insert_post的返回值分配所覆盖。

As is, the whiteboard post created is the one being decorated with the metadata and not the intended post. 照原样,创建的白板帖子是用元数据修饰的帖子,而不是预期的帖子。

You can fix this by using a different name for the variable referencing the return value from call to wp_insert_part . 您可以通过对变量使用不同的名称来解决此问题,该变量引用从对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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM