admin管理员组文章数量:1328052
Hello I wanna to ask how can we set a sub category of a post in wordpress programatically?
wp_set_object_terms( $post_id, 'Lense', 'product_cat', false);
wp_set_object_terms( $post_id, 'Canon', 'product_cat', true);
But what it does is not setting the parent and child category it set as both parent category
Hello I wanna to ask how can we set a sub category of a post in wordpress programatically?
wp_set_object_terms( $post_id, 'Lense', 'product_cat', false);
wp_set_object_terms( $post_id, 'Canon', 'product_cat', true);
But what it does is not setting the parent and child category it set as both parent category
Share Improve this question edited Jan 29, 2019 at 9:54 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jan 29, 2019 at 2:30 Yves GonzagaYves Gonzaga 53 bronze badges1 Answer
Reset to default 0You will have to first insert the term as required using wp_insert_term.
wp_set_object_terms( $post_id, 'Lense', 'product_cat', false);
This function checks if 'Lense' term exist in product_cat or not. If does not exist, then create and assign. Check whether term already exists or not
$id = term_exists( 'canon','product_cat',$parent_id ) //$parent_id is parent term id, in your case id of Lense
if($id == NULL)
{
$id = wp_insert_term(
'Canon', // the term
'product_cat', // the taxonomy
array(
'description'=> 'Test description',
'slug' => 'canon',
'parent'=> $parent_term['term_id'] // get numeric term id of parent - Lense
)
);
}
wp_set_object_terms( $post_id,$id, 'product_cat',true);
This will work.
// No space is allowed in taxonomy name
本文标签: How to set subcategory in Woocommerce
版权声明:本文标题:How to set subcategory in Woocommerce? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742241140a2438841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论