Answers:
使用wp_insert_post()和add_post_meta(),如下所示:
// insert the post and set the category
$post_id = wp_insert_post(array (
'post_type' => 'your_post_type',
'post_title' => $your_title,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed', // if you prefer
'ping_status' => 'closed', // if you prefer
));
if ($post_id) {
// insert post meta
add_post_meta($post_id, '_your_custom_1', $custom1);
add_post_meta($post_id, '_your_custom_2', $custom2);
add_post_meta($post_id, '_your_custom_3', $custom3);
}
'meta_input' => ['_your_custom_1' => $custom1, '_your_custom_2' => custom2]
除了 上述@webaware好答案之外,自wordpress 4.4.0起,所有这些都可以通过wp_insert_post调用来处理:
$post_id = wp_insert_post(array (
'post_content' => $content,
'post_title' => $title,
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
// some simple key / value array
'meta_input' => array(
'your_custom_key1' => 'your_custom_value1',
'your_custom_key2' => 'your_custom_value2'
// and so on ;)
)
));
if ($post_id) {
// it worked :)
}
可以很容易地使用 Gravity Forms插件。您可以构建一个在后端填充“自定义帖子类型”的表单。该帖子可以设置为草稿或已发布。添加自定义字段没有问题。就我而言,我用它来收集客户推荐。