[英]Functions Hook add_post_meta ACF Post Object
我在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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.