The WordPress function is used for submitting data programatically. Standard fields to submit to incude the content, excerpt, title, date and many more.
What there is no documentation for is how to submit to a custom field. I know it is possible with the add_post_meta($post_id, $meta_key, $meta_value, $unique);
function.
But, how to include that into the standard wp_insert_post
function?
<?php
$my_post = array(
'post_title' => $_SESSION['booking-form-title'],
'post_date' => $_SESSION['cal_startdate'],
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'booking',
);
wp_insert_post( $my_post );
?>
If you read the documentation for
wp_insert_post
, it returns the post ID of the post you just created.If you combine that with the following function
__update_post_meta
(a custom function I acquired from this site and adapted a bit)You’ll get the following:
You can simple add the ‘add_post_meta’ after the ‘wp_insert_post’
You can add ‘meta_input’ element on the post parameters as an array of post meta values keyed by their post meta key
I don’t think you can use it with wp_insert_post();.
The reason is because of how WP stores the two data types. Posts are stored in one big monolithic table with a dozen different columns (wp_posts); custom fields are stored in a simpler, 4-column table (wp_postmeta) comprised mainly of a meta key and value, associated with a post.
Consequently, you can’t really store custom fields until you have the post ID.
Try this:
This codex post helped — it’s kinda the opposite of what you’re doing (i.e., deleting a DB row upon post deletion): http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post
Use
save_post
filter, then calladd_post_meta
in your filter function.