How to export only custom taxonomies for each post in CSV/JSON format?

Question

I have one wordpress website running with 100+ products (woocommerce) and i had to export & import the products into another wordpress website. However, products have custom taxonomies that were imported into the new website, but not linked to the products (Woocommerce doesn’t allow to export custom taxonomies related to products with default importer/exporter).

The best approach i thought besides buying a premium plugin just for this purpose is to generate a .csv or .json file with all products and the terms attached to it, and then in the new website run something like that:


[...]
function wp_import_taxonomies(){
   
   $terms_arr = get_array_of_terms_from_csv_file($file); //Just for simplification

   $args = array(
      'post_type' => 'product',
      'numberposts' => -1
   );

   $products = get_posts($args);

   foreach($products as $product){
      foreach($terms_arr as $taxonomy => $terms){
          wp_set_post_terms($product->ID, $terms, $taxonomy);
      }
   }

}

Note: Taxonomies and terms are already created in the destination website. I just need to make the link between each product and taxonomy + terms.

Better solutions are also welcome.

Thanks in advance,

0
joann2555 4 months 0 Answers 12 views 0

Leave an answer