Programmatically publish a post (custom post type) with custom fields

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?


Use 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);

Leave a Comment