0

When processing the form i want the values to be inserted into the custom post type products.

Here is what i have done so far

$art_title = $_POST['art_title'];

$i = 0;
foreach($art_title as $value) {
$art_title = $_POST['art_title'][$i];
$art_width = $_POST['art_width'][$i];
$art_height = $_POST['art_height'][$i];
$art_price = $_POST['art_price'][$i];
$art_creation_date = $_POST['art_creation_date'][$i];

   // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $art_title,
        'post_status'   => 'draft',    
        'post_type' => 'products'
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    $attachment_id = media_handle_upload( 'art_upload[$i]', $pid );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

$i++;
}

I used media_handle_upload along with the post id but this isn't working.

What i want to know is how do i insert these $_POST values into the custom post type products along with the image as product_image?

The custom post type is the woocommerce products.

Any help greatly appreciated. An example would be great.

1 Answer 1

1

Your current code should be correctly creating the post inside your Products post type.

You're missing the bit that updates the post meta holding the image. Assuming the meta_key is product_image, then running this on success should do:

update_post_meta( $pid, 'product_image', $attachment_id );

on a side note: don't forget to run wp_generate_attachment_metadata and wp_update_attachment_metadata after you upload the image.

2
  • thanks for this, how do i find out the woocommerce meta_keys/custom field names?
    – user892134
    Dec 11, 2014 at 22:40
  • they probably have some documentation for this, but if all else fails, this plugin should help you find it: wordpress.org/plugins/database-peek
    – moraleida
    Dec 11, 2014 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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