0

I found some code on this site for wp_insert_post and I got it working. I did find the post in the category where I would expect it to be. Then I changed the code example to include information from my form on the page and the code fails.

Here is the sample code I got from this site that does work.

<?php if(isset($_POST['new_reservation']) == '1') {
    $post_title = "My Post";
    //$post_category = 'reservations';

    $my_post = array();
    $my_post['post_title'] = "My Post";
    $my_post['post_content']  = 'This is my post.';
    $my_post['post_status']   = 'publish';
    $my_post['post_author']   = 1;
    $my_post['post_category'] = array(9);

     wp_insert_post($my_post);
    //update_post_meta($post_id, 'reservations', $customer);

} ?>

so then I changed things to this in order to get the name of the post from the form

<form action="" method="post" name="reservations" id="reservations" enctype="multipart/form-data"> 

    <table width="474" border="0" cellspacing="0" cellpadding="0" id="reserve">
        <tr>
            <td class="formTitles"><img src="<?php bloginfo('template_directory');?>/images/name.png" alt="name" width="47" height="10" /></td>
            <td class="formTitles"><img src="<?php bloginfo('template_directory');?>/images/date.png" alt="date" width="43" height="10" /></td>
        </tr>
        <tr>
            <td class="formInput"><input type="text" id="name" name="name" /></td>

                <input type="hidden" name="new_reservation" value="1" /><br />
            <td><input type="submit" name="reserveSubmit" id="reserveSubmit" class="button" value="submit"/> <span class="regularText"> or <a class="clearForm" href="#" onclick="reservation.reset();">Clear Form</a> </span></td>
        </tr>

</table>

<?php if(isset($_POST['new_reservation']) == '1') {
    $post_title = $_POST['name'];
    $post_category = 'reservations';

    $my_post = array();
    $my_post['post_title'] = $post_title;
    $my_post['post_content']  = 'This is my post.';
    $my_post['post_status']   = 'publish';
    $my_post['post_author']   = 1;
    $my_post['post_category'] = array(9);// Insert the post into the databasewp_insert_post( $my_post );"



     wp_insert_post($my_post);
    //update_post_meta($post_id, 'reservations', $customer);

} ?>

The code fails whenever I try to use the name variable as the name of the post

  • 2
    Show us the complete code, with the wp_insert_post line as well. And, right before the wp_insert_post, do a var_dump($my_post); to see what is contained in the variable that you are inserting. – cale_b Mar 31 '12 at 1:33
  • @cale_b I just edited the code above to show it all – Jamie Mar 31 '12 at 3:10
0

Change 'name' to something else. Because name is a reserved keyword in WordPress universe. Check this and look for 'name' here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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