I’m working to create a front end custom post type submission from. I already coded checking different tuto. But the form returned not found page upon suvmission, again not inputing any data. Please help me to find out the error if possible.
<?php
if (!isset($_POST['submit'])) {
?>
<form method="post" action="<?php the_permalink(); ?>">
<table>
<tr><th width="138"><label for="id_meta">Mobile No:</label></th><td width="163"><input id="id_meta" type="text" name="wpcf-mob_no" maxlength="100" /></td></tr>
<tr><th width="138"><label for="id_meta">Amount To Load:</label></th><td width="163"><input id="id_meta" type="text" name="wpcf-mob_amount" maxlength="100" /></td></tr>
<tr><th width="138"><label for="id_meta">Connection Type</label></th><td width="163"><select id="id_meta" type="text" name="wpcf-mob_type" maxlength="100" />
<option value="pre_paid">Pre Paid</option>
<option value="post_paid">Post Paid</option>
</td></tr>
<tr><th width="138"></th><td width="163"><input id="id_meta" type="hidden" name="wpcf-mob_status" maxlength="100" value="pending"/></td></tr>
<tr><th></th><td><input id="id_title" type="hidden" name="post_title" maxlength="100" value="<?php echo date('r'); ?>"/></td></tr>
<tr>
<td> </td>
<input type="hidden" name="post_type" id="post_type" value="loads" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>
<td><input type="submit" value="Submit" name="submit"/></td>
</tr>
</table>
</form>
<?php }
else {
$title = $_POST['post_title'];
$meta_box1 = $_POST['wpcf-mob_no'];
$meta_box2 = $_POST['wpcf-mob_amount'];
$meta_box3 = $_POST['wpcf-mob_type'];
$meta_box4 = $_POST['wpcf-mob_status'];
$new_post = array(
'post_title' => $title,
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'loads' //'post',page' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
/* Insert Form data into Custom Fields */
add_post_meta($pid, 'wpcf-mob_no', $meta_box1, true);
add_post_meta($pid, 'wpcf-mob_amount', $meta_box2, true);
add_post_meta($pid, 'wpcf-mob_type', $meta_box3, true);
add_post_meta($pid, 'wpcf-mob_status', $meta_box4, true);
print '<pre>';
var_dump(array($new_post, $pid));
print '</pre>';
}
?>
Since the meta data field is already there you should update it.
also you should time the wp_insert_post… here is the revied code
try it and let me know it you encounter problems:
The code bellow worked for me.