Leave a Reply

2 comments

  1. 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.

  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.';
    }