Php 如何将WordPress媒体附件转换为WooCommerce产品

Php 如何将WordPress媒体附件转换为WooCommerce产品,php,wordpress,Php,Wordpress,我目前正在使用WooCommerce构建自己版本的stock.adobe网站。为了做到这一点,我必须将WordPress media选项卡中的所有图像文件转换为具有适当元数据的WooCommerce产品 下面我编写了一个PHP脚本来实现这一点。我编写的脚本成功地创建了新的WooCommerce产品,但由于某些原因,我无法使用set\u post\u缩略图()WordPress函数设置帖子的产品图像 以下是脚本: <?php function createWooPost($name, $id

我目前正在使用WooCommerce构建自己版本的stock.adobe网站。为了做到这一点,我必须将WordPress media选项卡中的所有图像文件转换为具有适当元数据的WooCommerce产品

下面我编写了一个PHP脚本来实现这一点。我编写的脚本成功地创建了新的WooCommerce产品,但由于某些原因,我无法使用
set\u post\u缩略图()
WordPress函数设置帖子的产品图像

以下是脚本:

<?php
function createWooPost($name, $id){
    $newProductInfo = array(
      "post_author" => 1,
      "post_content" => "Another wonderful image from one of our talented photographers.",
      "post_title" => $name,
      "post_parent" => "",
      "post_type" => "product"
    );
      
    $newProduct = wp_insert_post($newProductInfo);
    
   //creating meta data for this new post
    update_post_meta( $newProduct, '_visibility', 'visible' );
    update_post_meta( $newProduct, '_stock_status', 'instock');
    update_post_meta( $newProduct, 'total_sales', '0');
    update_post_meta( $newProduct, '_downloadable', 'yes');
    update_post_meta( $newProduct, '_virtual', 'yes');
    update_post_meta( $newProduct, '_regular_price', "1" );
    update_post_meta( $newProduct, '_sale_price', "1" );
    update_post_meta( $newProduct, '_purchase_note', "" );
    update_post_meta( $newProduct, '_featured', "no" );
    update_post_meta( $newProduct, '_weight', "" );
    update_post_meta( $newProduct, '_length', "" );
    update_post_meta( $newProduct, '_width', "" );
    update_post_meta( $newProduct, '_height', "" );
    update_post_meta( $newProduct, '_sku', "");
    update_post_meta( $newProduct, '_product_attributes', array());
    update_post_meta( $newProduct, '_sale_price_dates_from', "" );
    update_post_meta( $newProduct, '_sale_price_dates_to', "" );
    update_post_meta( $newProduct, '_price', "20" );
    update_post_meta( $newProduct, '_sold_individually', "" );
    update_post_meta( $newProduct, '_manage_stock', "no" );
    update_post_meta( $newProduct, '_backorders', "no" );
    update_post_meta( $newProduct, '_stock', "" );
  
    //setting featured image for product
    set_post_thumbnail( $newroduct -> ID, $id);
  }

  function getImages(){
    $args = array(
      "post_type" => "attachment",
      "post_status" => "inherit",
      "posts_per_page" => -1
    );
    
    $attachments = new WP_Query($args);
    $images = $attachments -> get_posts();
    
    for($i = 0; $i < count($images); $i++){
      $image_name = $images[$i] -> post_title;
      $image_ID = $images[$i] -> ID;

    //At this point I have access to every name and url of every attachment
    //Now I need to create a woocommerce post and insert this information
      createWooPost($image_name, $image_ID);
    }
  }

  getImages();
?>

所以我要做的是获取图像的附件id,并将其传递给创建WooCommerce产品的函数,然后将该图像设置为该产品的产品图像


有人能看看我的代码并帮我找出我做错了什么吗?

当你调用
设置帖子缩略图时,它会返回什么?