如果您阅读文档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\' );