這裏提出的解決方案可以讓我輕鬆了WordPress郵寄打造「分類」:如何以編程方式設置新Woocommerce產品創建的類別?
//Check if category already exists
$cat_ID = get_cat_ID($category);
//If it doesn't exist create new category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $category);
wp_insert_category($cat_name);
}
//Get ID of category again incase a new one has been created
$new_cat_ID = get_cat_ID($category);
// Create post object
$new_post = array(
'post_title' => $headline,
'post_content' => $body,
'post_excerpt' => $excerpt,
'post_date' => $date,
'post_date_gmt' => $date,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($new_cat_ID)
);
// Insert the post into the database
wp_insert_post($new_post);
然而,Woocommerce不承認這些類別。 Woocommerce類別存儲在其他地方。如何以編程方式爲woocommerce創建類別,以及將其分配給新帖子的正確方法是什麼?
如何添加'product_cat'本身? – 2017-05-19 18:53:56