如何在wooccommerce wordpress中添加产品类别


How to add a product category in woocommerce wordpress

我真的希望有人能尽快帮我解决这个问题。

好吧,所以我正在为用户构建一个costum脚本来发布新产品,我已经完成了所有工作并成功插入(事件照片),但似乎找不到应该使用什么代码来更新帖子类别,因为这不是一个正常的类别,因为它有"product_cat"(wooccommerce产品类别)的分类法。

有什么想法吗?

非以下工作:($term_id是与某个产品的"product_cat"相关的term_id)

wp_set_post_terms($post_id,数组($term_id),"product_cat");wp_set_post_terms($post_id,(int)$term_id,"product_cat");update_post_meta($post_id,'product_cat',$term_id);

我尝试过其他函数,但它们似乎什么都不做,有些函数甚至用id创建了一个新的类别…

您可以使用:

wp_set_post_terms(
    int $post_id, 
    string|array $tags = '', 
    string $taxonomy = 'post_tag', 
    bool $append = false
)

参数:

$post_id(int)(可选)Post ID。不默认为全局$Post的ID。

$tags(string|array)(可选)要为文章设置的术语数组,或用逗号分隔的术语字符串。层次分类法必须始终传递ID而不是名称,这样就不会混淆具有相同名称但不同父母的孩子。默认值:"

$taxonomy(string)(可选)分类名称。

默认值:"post_tag"

$append(bool)(可选)如果为true,则不删除现有术语,只添加。如果为false,则用新术语替换这些术语。

默认值:错误

点击此处了解更多信息:https://developer.wordpress.org/reference/functions/wp_set_post_terms/

试用wp_insert_term:

wp_insert_term(
  'New Category', // the term 
  'product_cat', // the taxonomy
  array(
    'description'=> 'Category description',
    'slug' => 'new-category'
  )
);