Alex Knopp
Alex Knopp

Reputation: 935

Problem with custom taxonomies using wp_insert_post()

I have an array Im passing to the tax_input argument in wp_insert_post(). Below is a result of printing the wp_insert_post() arguments.

[tax_input] => Array
    (
        [knpvsoftcomp] => Array
            (
                [0] => 1530
            )

        [knpvfiletype] => Array
            (
                [0] => 1497
            )

        [product_cat] => Array <-- Woo Product Categories
            (
                [0] => 821
                [1] => 829
                [2] => 1530
                [3] => 908
            )

        [pa_designer] => Array
            (
                [0] => 549
            )

    )

Here is the function inserting the new post.

$newpostargs = [
        'post_type' => 'product',
        'post_title' => sanitize_text_field($this->submission['fieldvalues']['product_title']),
        'post_author' => $this->currentUser->ID,
        'post_content' => $this->submission['fieldvalues']['description'],
        'post_status' => 'draft',
        'tax_input' => $this->knpv_tax_input(),
        'meta_input' => $this->knpv_meta_input()    
    ];
    $newpost = wp_insert_post($newpostargs);

Here is the function being called to create the taxonomy array.

public function knpv_tax_input(){

    $taxarray = [];
    $productcategories = [];

    foreach ($this->submission['fieldvalues'] as $key => $value) {

        //For product_categories
        if (strpos($key, 'cat-parent') !== false || strpos($key, 'catchild-') !== false) {
            $productcategories[] = $value;
        }

        //Software version
        if (strpos($key, 'software') !==  false) {
            $taxarray['knpvsoftcomp'] = [$value];
        }

        //File Types
        if (strpos($key, 'file-compat') !== false) {
            $taxarray['knpvfiletype'] = [$value];
        }           

    }

    //Add product categories to tax array
    $productcategories[] = 908;
    $taxarray['product_cat'] = $productcategories;


    //Supplier (Vendor) Taxonomy
    $taxarray['pa_designer'] = [$this->submission['vendor']['vendor_id']];

    return $taxarray;

}

All of these are custom taxonomies expect for the Woo Product Categories. However, these are the only terms being assigned to the post. Any thoughts?

Im admin on the site so all permissions are set right.

Upvotes: 0

Views: 2165

Answers (1)

vadivel a
vadivel a

Reputation: 1794

Replace '$term_id' to your term ID Hope this help you.

// Creates woocommerce product 
$product = array(
    'post_title'    => $name,
    'post_content'  => '',
    'post_status'   => 'publish',
    'post_author'   => $current_user->ID,
    'post_type'     =>'product'
);

// Insert the post into the database
$product_ID = wp_insert_post($product);
wp_set_object_terms($product_ID, $term_id, 'product_cat')

;

//you-want-multiple-cat

$categories = [ 'Some Category', 'Some other Category' ];
// Overwrite any existing term
wp_set_object_terms( $product_id, $categories, 'product_cat' );

// Or, set last argument to true to append to existing terms
wp_set_object_terms( $product_id, $categories, 'product_cat', true );

Upvotes: 2

Related Questions