4

I am having trouble creating a woocommerce product category programmatically. I plan to automate the creation of product categories based on the creation of a new term in a specified custom taxonomy. Any help is greatly appreciated, Thank you.

1
  • What exactly are you trying to do? What is your question? Commented Jul 17, 2018 at 20:50

1 Answer 1

14

To create a taxonomy term programmatically you can use wp_insert_term function.

<?php wp_insert_term( $term, $taxonomy, $args = array() ); ?>

It has 3 params:

$term (int|string) (required) The term to add or update. Default: None

$taxonomy (string) (required) The taxonomy to which to add the term. Default: None

$args (array|string) (optional) Change the values of the inserted term Default: None

WooCommerce categories are stored as terms in product_cat taxonomy, so if you want to create some new category, you can use this code:

wp_insert_term( 'My New Category', 'product_cat', array(
    'description' => 'Description for category', // optional
    'parent' => 0, // optional
    'slug' => 'my-new-category' // optional
) );
1
  • Thank you! Was stuck on this for hours. I greatly appreciate the help.
    – Tyler
    Commented Jul 18, 2018 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.