0

In which database table and column do I need to insert an image path, in order to display product images in wp-admin (not on the front end)? I insert products by query in WordPress, rather than insert products by admin.

$insert = $wpdb->insert( $prefix."posts", array(
    "post_title"   => $posTitle,
    "post_content" => $postContent,
    "post_status"  => "publish",
    "post_type"    => "product"
) );
// select products ID
$select = $wpdb->get_results( "SELECT ID AS productsId 
    FROM " . $prefix . "posts 
    WHERE post_title='" . $posTitle . "'" );
// echo $select[0]->productsId; die();
// insert umage in posts and product attribute meta posts table
$insertImg = $wpdb->insert( $prefix . "posts", array( 
    "post_status" => "inherit",
    "post_type"   => "attachment",
    "guid"        => $postGuid,
    "post_parent" => $select[0]->productsId 
) );

2 Answers 2

1

While this is theoretically possible it is going to be very unpleasant to get right.

The "image" in WordPress view would be attachment post with considerable amount of metadata, recording information about physical file for image itself, image sizes, EXIF info from image, and so on.

You should really be using API for task like this, I would start with looking at media_handle_sideload().

PS wp_insert_post() for creating normal posts would be practical as well.

0
$image_url = $review['image'];

$tmp = download_url( $image_url );

preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $image_url, $matches);

$file_array['name'] = basename($matches[0]);

$file_array['tmp_name'] = $tmp;

$thumbid = media_handle_sideload( $file_array, $new_post_id, 'gallery desc' );

set_post_thumbnail($new_post_id, $thumbid);

update_post_meta( $new_post_id, '_product_image_gallery',  $thumb_url);

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.