最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to set subcategory in Woocommerce?

programmeradmin11浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

You 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

发布评论

评论列表(0)

  1. 暂无评论