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.

I'm trying to use wp_insert_post to create a front-end submission form so visitors can create a post AND update the meta box fields of that post -- not the custom fields.

For example, when I use the following in the submission form...

<?php update_post_meta($post_id, $meta_key, $meta_value); ?>

...it updates the built-in custom field section of the newly created post, but not the meta boxes that I created. Any ideas?

share|improve this question
    
You can use 'update_post_meta' function, just replace $meta_key with the key of your meta boxes –  booota Aug 21 '11 at 20:05
    
@booota The above function is an example. I am replacing the attributes with my own key names and values. Thanks for the suggestion though! –  Chris Molitor Aug 21 '11 at 20:08
    
oh ok... You can use add_post_meta just after creating the post. I dont think you can add meta using wp_insert_post in one go... –  booota Aug 21 '11 at 20:22

2 Answers 2

Here is some code that I've used to add posts from a front end form that also adds meta data and taxonomy terms. Note that snippet extracts all of the security, data validation and data sanitization. It just show what I do with the data to add it to the database once I've sanitized it all.

// Submit the values if there are no errors
if(empty($errors))
{
    // Prepare title
    $term = get_term_by('id', $values['complaint_type'], 'complaint-type');
    $title = $values['address_clean'].' ('.$term->name.')';

    // Gather post data
    $post = array(
        'post_title' => $title,
        'post_content' => $values['description'],
        'post_status' => 'publish', 
        'post_type' => PREFIX_POST_TYPE_NAME,
        'post_author' => 1
    );

    // Attempt to add post
    if($id = wp_insert_post($post))
    {
        // Add metadata to post
        update_post_meta($id, '_'.PREFIX_PLUGIN_NAME_L.'_latitude', $values['latitude']);
        update_post_meta($id, '_'.PREFIX_PLUGIN_NAME_L.'_longitude', $values['longitude']);

        // Associate complaint-type
        if(!wp_set_post_terms($id, $values['complaint_type'], 'complaint-type'))
            $errors['wp_set_post_terms'] = 'There was an error adding the complaint type.';
    }
    else
        $errors['wp_insert_post'] = 'There was an error adding the complaint.';
}
share|improve this answer

It's all going into the same table, you're possibly not using the correct key name to insert it. The only thing that differentiates custom field keys and keys used for metaboxes is that metabox keys are typically prefixed with an underscore to hide them from the list that shows up in the custom fields section. it's also possible to store multiple things as an array under a single entry. that may be the case if you're using a metabox helper class of some sort to generate the metaboxes. look directly in the table to see how your metabox data is being stored.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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