我在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 );这是因为它创建了一个白板post类型的post --但它不会更新我的post对象字段(project_select)。如果我指定一个ID:
add_post_meta($post_id, 'project_select', '1', true);然后它就可以工作了--我的问题是如何将刚刚创建的帖子的ID传递给它?
发布于 2018-05-18 11:05:24
来自wp_insert_post的返回值的赋值将覆盖$post_id。
按照原样,创建的白板帖子是用元数据装饰的帖子,而不是预期的帖子。
您可以通过为引用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);
}https://stackoverflow.com/questions/50403119
复制相似问题