if term exists problem with wp_insert_term
-
Hi I hope somebody will be able to help me, I am building a site which allows users to add products from the front end to a woocommerce its all working well except I am not able to check whether a product category exists and if it does link the product to it or if doesn’t create the term/ product category and link it to it (allowing for multiple categories below is the code which I have
foreach ($mycategories as $mycat){ $myterm = term_exists($mycat->Name, 'product_cat'); if ($myterm !== 0 && $myterm !== null) { $cat_ID_array = get_term_by( 'name', $cat->Name, 'product_cat'); $cat_ID = $cat_ID_array->term_id; } else { $cat_name = $cat->Name; $catid = wp_insert_term($cat_name, 'product_cat'); <em>$cat_ID = $catid['term_id'];</em> } echo '<input type="checkbox" name="prod_category[]" checked value="'.$cat_ID.'" ><label>'.$cat->Name.'</label>'; } foreach ($subcategories as $subcat){ $subterm = term_exists($subcat->Name, 'product_cat'); if ($subterm !== 0 && $subterm !== null) { $subcat_ID_array = get_term_by( 'name', $subcat->Name, 'product_cat'); $subcat_ID = $subcat_ID_array->term_id; } else { $subcat_name = $subcat->Name; $subcatid = wp_insert_term($subcat_name, 'product_cat'); $subcat_ID = $subcatid['term_id']; } echo '<input type="checkbox" name="prod_category[]" checked value="'.$subcat_ID.'" ><label>'.$subcat->Name.'</label>'; }
I am getting an error on the line = $cat_ID = $catid[‘term_id’];
Fatal error: Cannot use object of type WP_Error as array in…..formsubmission.php on line 226
Can anyone point me in the right direction please ?
- The topic ‘if term exists problem with wp_insert_term’ is closed to new replies.