admin管理员组文章数量:1426637
I have a custom post type 'Participant' with many custom fields. I also have a form with corresponding input fields for the user to fill out. When he submits the form, I want a new post to be generated with each custom fields containing the value chosen by the user.
Is it possible to do and if so, how?
I have a custom post type 'Participant' with many custom fields. I also have a form with corresponding input fields for the user to fill out. When he submits the form, I want a new post to be generated with each custom fields containing the value chosen by the user.
Is it possible to do and if so, how?
Share Improve this question asked Dec 25, 2012 at 7:43 drake035drake035 1,2177 gold badges34 silver badges57 bronze badges3 Answers
Reset to default 45Use wp_insert_post() and add_post_meta(), like this:
// 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);
}
In addition to the great answer of @webaware above, this can be handled since wordpress 4.4.0 all via the wp_insert_post call:
$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 :)
}
This can be achieved quite easily using the Gravity Forms plugin. You can build a form which populates a Custom Post Type in the backend. This post can be set to appear as a draft or as published. No problem adding custom fields. In my case, I used it to gather client testimonials.
本文标签: Programmatically publish a post (custom post type) with custom fields
版权声明:本文标题:Programmatically publish a post (custom post type) with custom fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745471790a2659783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论