如果您阅读的文档wp_insert_post
,它将返回您刚刚创建的帖子的帖子ID。
如果您将其与以下功能结合使用__update_post_meta
(我从此站点获得的自定义功能并做了一些修改)
/**
* Updates post meta for a post. It also automatically deletes or adds the value to field_name if specified
*
* @access protected
* @param integer The post ID for the post we're updating
* @param string The field we're updating/adding/deleting
* @param string [Optional] The value to update/add for field_name. If left blank, data will be deleted.
* @return void
*/
public function __update_post_meta( $post_id, $field_name, $value = '' )
{
if ( empty( $value ) OR ! $value )
{
delete_post_meta( $post_id, $field_name );
}
elseif ( ! get_post_meta( $post_id, $field_name ) )
{
add_post_meta( $post_id, $field_name, $value );
}
else
{
update_post_meta( $post_id, $field_name, $value );
}
}
您将获得以下内容:
$my_post = array(
'post_title' => $_SESSION['booking-form-title'],
'post_date' => $_SESSION['cal_startdate'],
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'booking',
);
$the_post_id = wp_insert_post( $my_post );
__update_post_meta( $the_post_id, 'my-custom-field', 'my_custom_field_value' );