'Wordpress - Woocommerce wp_insert_post() 'post_category'
I'm trying to import woocommerce products with wp_insert_post()
function but i have problem with 'post_category'
. My code is
$my_post = array(
'post_content' => $description,
'post_name' => $product_name,
'post_title' => $product_name,
'post_status' => 'publish',
'post_type' => 'product',
'post_author' => 1,
'post_category' => array(9,10)// ids from woocommerce categories
);
wp_insert_post( $my_post );
But categories are empty. I tried 'product_cat' => array(9,10)
but nothing again. Can anyone help me?
Solution 1:[1]
You should use slug instead id:
wp_set_object_terms($post_id, get_term(9)->slug, 'product_cat', true);
wp_set_object_terms($post_id, get_term(16)->slug, 'product_cat', true);
If you put here new category slug, WordPress will create it, and attached product to this category.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Suraj Rao |