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 );
?>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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)
/**
* Updates post meta for a post. It also automatically deletes or adds the value to field_name if specified
*
* @access protected
* @param integer The post ID for the post we're updating
* @param string The field we're updating/adding/deleting
* @param string [Optional] The value to update/add for field_name. If left blank, data will be deleted.
* @return void
*/
public function __update_post_meta( $post_id, $field_name, $value = '' )
{
if ( empty( $value ) OR ! $value )
{
delete_post_meta( $post_id, $field_name );
}
elseif ( ! get_post_meta( $post_id, $field_name ) )
{
add_post_meta( $post_id, $field_name, $value );
}
else
{
update_post_meta( $post_id, $field_name, $value );
}
}
You’ll get the following:
$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',
);
$the_post_id = wp_insert_post( $my_post );
__update_post_meta( $the_post_id, 'my-custom-field', 'my_custom_field_value' );
Method 2
You can simple add the ‘add_post_meta’ after the ‘wp_insert_post’
<?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',
);
$post_id = wp_insert_post($my_post);
add_post_meta($post_id, 'META-KEY-1', 'META_VALUE-1', true);
add_post_meta($post_id, 'META-KEY-2', 'META_VALUE-2', true);
?>
Method 3
You can submit meta_input as an – (array) Array of post meta values keyed by their post meta key
<?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',
'meta_input' => array(
'your_custom_meta_field_name' => $_SESSION['your_custom_meta_field_value']
)
);
wp_insert_post( $my_post );
?>
Method 4
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:
function myplugin_insert_customs($pid){
$customs = array(
'post_id' => $pid,
'meta_key' => 'Your meta key',
'meta_value' => 'Your meta value',
);
add_post_meta($customs);
}
add_action('save_post', 'myplugin_insert_customs', 99);
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
Method 5
Use save_post filter, then call add_post_meta in your filter function.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0