Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

What I don't know is 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 );
  ?>

Any ideas,

Marvellous

share|improve this question
add comment

4 Answers

up vote 17 down vote accepted

Hi @Robin I Knight

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' );
share|improve this answer
    
Thanks very much. Could you possibly give me an idea on the implantation. IE. which but of code goes where. Many Thanks –  Robin I Knight Feb 4 '11 at 21:13
2  
Nicely done. The second code block replaces yours, the function values is the custom field key/value pair. Put the function either at the top of the script, or in a separate .php file included at the top of the script. –  aendrew Feb 4 '11 at 21:37
    
I couldn't have put it any better than aendrew honestly. +1 –  Zack Feb 4 '11 at 21:44
    
As a note, I do use OOP so that's the reason for the public modifier in front of "function". If you're including the function itself without putting it into a class, you don't need to add public –  Zack Feb 5 '11 at 3:13
    
Bloody marvellous. My hat goes off to you. –  Robin I Knight Feb 5 '11 at 14:16
show 6 more comments

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);
?>
share|improve this answer
add comment

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

share|improve this answer
    
In that case the only way out I can see is to use a session, would that be correct. –  Robin I Knight Feb 4 '11 at 20:42
    
Nah; I'm guessing your plugin is trying to insert custom fields at the same time a post is saved, right? I think what you need to do is hook into WP after the post is saved, grab the post's new ID number, then supply that to add_post_meta(); to create the CFs. I'll update my answer in a second with some code. –  aendrew Feb 4 '11 at 20:48
    
Thanks for the help. By the way its not a plugin. I wrote it so we can customise it as much as needed. (but don't take that to mean I'm any good with php, just trial and error) –  Robin I Knight Feb 4 '11 at 20:52
    
It's a theme, then? Only real difference is you'd put that in functions.php, in that case. –  aendrew Feb 4 '11 at 21:00
    
See Zack's answer, way more specific. –  aendrew Feb 4 '11 at 21:40
add comment

Use save_post filter, then call add_post_meta in your filter function.

derp andy

share|improve this answer
    
Unhelpful. $post->ID is not available to wp_insert_post_data, which is necessary for creating custom fields. –  aendrew Feb 4 '11 at 20:40
    
@aendrew save_post action is at the very end of the function, it has post's ID and object passed to it, answer is sound. –  Rarst Feb 5 '11 at 0:16
1  
I'm pretty sure this was edited, Rarst. Regardless, it makes sense now. –  aendrew Feb 5 '11 at 2:04
    
@aendrew ah, sorry - didn't notice that –  Rarst Feb 5 '11 at 9:46
add comment

protected by Community Nov 8 '11 at 7:24

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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