通过自定义前端表单设置Woocommerce产品标签和类别

use*_*981 7 php tags wordpress categories woocommerce

我正在构建一个表单供用户使用wp_insert_post和通过我的网站前端创建产品update_post_meta.

尝试设置产品类别和标签时出现问题.在这方面,Woocommerce似乎没有使用标准的Wordpress分类法.有人对此有经验吗?Woocommerce似乎product_tags在某些地方使用.有没有像Wordpress一样创建它们的功能?

下面是我正在做的事情的片段.谢谢!

$post = array(
 'ID' => '',
 'post_content' => $_POST['post_content'],
 'post_title' => $_POST['post_title'],
 'post_status' => 'draft',
 'post_type' => 'product',
 'post_author' => $user_id,
);

$newListing = wp_insert_post($post, $wp_error);

//SET META
update_post_meta($newListing, '_stock_status', 'instock', true);
update_post_meta($newListing, '_visibility', 'visible', true);
update_post_meta($newListing, '_price', $_POST['_regular_price'], true);

//SET CATEGORIES - **NOT WORKING**
wp_set_post_categories($newListing, $categories);

//SET THE TAGS **NOT WORKING**
wp_set_post_tags($newListing, $tags, true);
Run Code Online (Sandbox Code Playgroud)

use*_*981 11

发现内置的Wordpress功能wp_set_object_terms将很容易处理.

以下是一些例子:

//SET THE PRODUCT CATEGORIES
wp_set_object_terms($productID, array('Cat Name 1', 'Cat Name 2'), 'product_cat');

//SET THE PRODUCT TAGS
wp_set_object_terms($productID, array('tag1','tag2','tag3'), 'product_tag');
Run Code Online (Sandbox Code Playgroud)