[英]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.