Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用新的缩略图PHP设置Woocommerce产品缩略图_Php_Wordpress_Woocommerce_Thumbnails_Product - Fatal编程技术网

使用新的缩略图PHP设置Woocommerce产品缩略图

使用新的缩略图PHP设置Woocommerce产品缩略图,php,wordpress,woocommerce,thumbnails,product,Php,Wordpress,Woocommerce,Thumbnails,Product,我已经创建了一个自定义插件来导入和导出woocommerce产品。我面临一个问题,当我尝试导入CSV时,产品的缩略图被更改并设置为先前添加到产品中的旧缩略图 我在CSV中有完整的图像URL。这是我的进口代码: $product_img = $row[13]; //Product Image $res = $wpdb->get_results('select post_id from ' . $wpdb->prefix . 'postmeta where meta_va

我已经创建了一个自定义插件来导入和导出woocommerce产品。我面临一个问题,当我尝试导入CSV时,产品的缩略图被更改并设置为先前添加到产品中的旧缩略图

我在CSV中有完整的图像URL。这是我的进口代码:

    $product_img = $row[13]; //Product Image
    $res = $wpdb->get_results('select post_id from ' . $wpdb->prefix . 'postmeta where meta_value like "%' . basename($product_img). '%"');
    if(count($res) == 0)
    {
        $res = $wpdb->get_results('select ID as post_id from ' . $wpdb->prefix . 'posts where guid="' . $product_img . '"');
    }
    $thumbnail_id = $res[0]->post_id;

    set_post_thumbnail( $product_id, $thumbnail_id );
    $attach_data = wp_generate_attachment_metadata( $thumbnail_id, $product_img );
    wp_update_attachment_metadata( $thumbnail_id,  $attach_data );
谁能告诉我哪里错了,为什么缩略图会用旧的更新。我正在尝试查找上一天的问题,但尚未找到


提前谢谢。

我找到了bug。第一个查询返回所有匹配的结果,并在选择第一个帖子id时返回第一个匹配的帖子id。代码如下:

$product_img = $row[13]; //Product Image

    $res = $wpdb->get_results('select ID as post_id from ' . $wpdb->prefix . 'posts where guid="' . $product_img . '"');

$thumbnail_id = $res[0]->post_id;

set_post_thumbnail( $product_id, $thumbnail_id );
这个很好用