I am wanting to take the following form, and use $wpdb to insert it into a post. I have tried reading the class reference page INSERT rows, but I don't really know what should be referenced. I am not sure how to tell the form to insert the data into a post using $wpdb?

if (isset($_POST['pickup-form']))
    // Clean POST data
    $demo_input = (isset($_POST['demo-input'])) ? trim((string) $_POST['demo-input']) : '';

    // Validate POST data
    if ('' === $demo_input)
        $errors['demo_input'] = __('Demo field is required');

    // No errors
    if (empty($errors))
        // Save POST data to database, send some e-mails, etc.
        $success = TRUE;

<?php if ( ! empty($success)) { ?>

    <p>Thank you!</p>

<?php } else { ?>

    <?php if ( ! empty($errors)) { ?>
        <?php print_r($errors) ?>
    <?php } ?>

    <form method="post" action="">
        <input type="hidden" name="pickup-form">
        <input type="text" name="demo-input" value="<?php if (isset($demo_input)) { echo esc_attr($demo_input); } ?>">
        <input type="submit">

<?php } ?>
  • What do you mean by "insert the form into a post", you mean inserting a NEW post using the form's field values?
    – Shane
    Commented Jan 25, 2012 at 18:56
  • yes, a new post using the forms field values. Commented Jan 25, 2012 at 19:04

1 Answer 1


In the success part of your code, you can build an array representing a post, and use wp_insert_post as such :


 $mypost = array(
      'post_title' => 'My Title',
      'post_type' => 'page'
      //... add other fields according to your form

 $mypost_id = wp_insert_post( $mypost ); //Returns new post id on success

Any field you don't specify will be filled by WordPress automatically.


For custom fields, see add_post_meta :

 $mypost_id = wp_insert_post( $mypost ); //SEE ABOVE
 $meta_key = 'your-new-field-name';
 $meta_value = 'your-form-value';
 $unique = true; // or false     

 add_post_meta( $mypost_id, $meta_key, $value, $unique );

Source : wp_insert_post

  • what would I put if I wanted to insert the form field values into a custom fields. I guess I would also have to create those custom fields not sure how to do that either. Commented Jan 25, 2012 at 19:38
  • See my revised answer.
    – Shane
    Commented Jan 25, 2012 at 19:44
  • sorry if I am a little bit slow, I am not sure what to place in the $meta_key and $meta_value Commented Jan 25, 2012 at 20:58
  • @AndersKitson $meta_key is the name of your custom field, $meta_value is it's value. Example, if you wanted to add a custom field 'subtitle' for a post : add_post_meta( $post_id, 'subtitle', 'This is my subtitle text', true );
    – Shane
    Commented Jan 25, 2012 at 21:04
  • 1
    I just tested wp_insert_post and add_post_meta in page.php, works fine for me. Did you look at the codex pages I linked to, they have better examples than I do.
    – Shane
    Commented Jan 26, 2012 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.