i written a script in woocmmerce to import 1000 simple product my script is working fine and it's added all the product at my wordpress site however the products are not showing in front end store. when i open the single product it show the product with prices but when i search the category it's not showing

when i edit the product and update it from admin panel by clicking update button without changing anything then the product display at my store i am not able to figure out the issue please help NOTE 1. i have already tried deleting Transients and hence it's not working

here is my Code

$querystr = "SELECT * FROM `table_name`";$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach($pageposts as $productpost) {
            $post = array(
                     'post_title'   => $productpost->attribute,
                     'post_excerpt' => '<pre> '.$productpost->porduct_name."<br/>".$productpost->description.' <pre>',
                     'post_status'  => "publish",
                     'post_content' => 'test',
                     'post_name'    => $productpost->attribute, //name/slug
                     'post_type'    => "product"
                    $new_post_id = wp_insert_post( $post);
                    wp_set_object_terms ($new_post_id,'simple','product_type');
                    wp_set_object_terms( $new_post_id, 2321, 'product_cat');
                    update_post_meta( $new_post_id, '_stock_status', 'instock');
                    update_post_meta( $new_post_id, '_weight', "0.06" );
                    update_post_meta( $new_post_id, '_sku', $productpost->sku);
                    update_post_meta( $new_post_id, '_stock', "100" );
                    update_post_meta($new_post_id, '_regular_price', (float)$productprice);
                    update_post_meta($new_post_id, '_price', (float)$productprice);
                    require_once(ABSPATH . 'wp-admin/includes/media.php');
                    require_once(ABSPATH . 'wp-admin/includes/file.php');
                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    $thumb_url = "image_path.jpg";

                    if (@getimagesize($thumb_url)) {
                    $tmp = download_url( $thumb_url );
                    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches);
                    $file_array['name'] = basename($matches[0]);
                    $file_array['tmp_name'] = $tmp;
                    if ( is_wp_error( $tmp ) ) {
                    $file_array['tmp_name'] = '';
                    $logtxt .= "Error: download_url error - $tmp\n";
                    $logtxt .= "download_url: $tmp\n";
                    //use media_handle_sideload to upload img:
                    $thumbid = media_handle_sideload( $file_array, $new_post_id, 'gallery desc' );
                    // If error storing permanently, unlink
                    if ( is_wp_error($thumbid) ) {
                    //return $thumbid;
                    $logtxt .= "Error: media_handle_sideload error - $thumbid\n";
                    $logtxt .= "ThumbID: $thumbid\n";
                    set_post_thumbnail($new_post_id, $thumbid);
                update_post_meta( $new_post_id, '_visibility', 'visible' );
                update_post_meta($new_post_id, 'post_status', 'publish');

1 Answer 1


Set product_visibility to visible, _stock_status key to instock, _stock key to some value also post_date and post_date_gmt set to date lower than today's date.

After placing this in your script your simple product become visible on shop page.

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.