问题描述
我真的希望有人能尽快解决我的问题.
I really hope someone can help me with this problem asap.
好吧,所以我正在为用户构建一个Costum脚本以发布新产品,我可以正常工作并成功插入(活动照片),但是似乎找不到任何地方应该使用什么代码来更新帖子类别,例如它不是普通类别,因为它具有"product_cat"(woocommerce产品类别)的分类法.
Ok so I am building a costum script for users to publish a new product, I have everything working and inserting successfully (Event the photo) but just cant seem to find anywhere what code should be used to update the post category, as it is not a normal category because it has the taxonomy of "product_cat" (woocommerce product category).
有什么想法吗?
没有以下工作:($ term_id是与某个产品的"product_cat"相关的term_id)
Non of the following work: ($term_id is the term_id that relates to the "product_cat" of a certain product)
wp_set_post_terms($post_id, array($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创建了一个新类别...
I have tried others but they just dont seem to do anything at all, some functions even create a new category with the id...
推荐答案
您可以使用: wp_set_post_terms(int $ post_id,string | array $ tags ='',字符串$ taxonomy ='post_tag',bool $ append = false)
参数:
$ post_id(int)(可选)帖子ID.不默认为全局$ post的ID.
$post_id(int) (Optional) The Post ID. Does not default to the ID of the global $post.
$个标签(string | array)(可选)要为帖子设置的术语数组,或由逗号分隔的术语字符串.分层分类法必须始终传递ID而不是名称,以使名称相同但父母不同的孩子不会感到困惑.默认值:"
$tags(string|array) (Optional) An array of terms to set for the post, or a string of terms separated by commas. Hierarchical taxonomies must always pass IDs rather than names so that children with the same names but different parents aren't confused.Default value: ''
$分类(字符串)(可选)分类名称.
$taxonomy(string) (Optional) Taxonomy name.
默认值:"post_tag"
Default value: 'post_tag'
$ append(布尔)(可选)如果为true,则不要删除现有术语,只需添加即可.如果为假,请用新条款替换条款.
$append(bool) (Optional) If true, don't delete existing terms, just add on. If false, replace the terms with the new terms.
默认值:false
在此处阅读有关此内容的更多信息: https://developer.wordpress.org/reference/functions/wp_set_post_terms/
Read more about it here:https://developer.wordpress.org/reference/functions/wp_set_post_terms/
这篇关于如何在woocommerce wordpress中添加产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!