I want to change the value of a custom taxonomy in woocommerce.
function get_pw_by_email($email) {
$products = get_posts(array('post_type' =>'product', 'tax_query' =>array(array('taxonomy' =>'pa_e-mail', 'field' =>'name', 'terms' =>$email))));
$terms = wp_get_post_terms($products[0] ->ID, 'pa_pw', true);
if (empty($terms)) {
return '';
}
return $terms[0]->name;
}
Like this I can get the correct taxonomy value. But now I want to create a random number and update that value - without loosing all other other taxonomies of that product.
CodePudding user response:
If you're doing this on a per-product basis, and not modifying the global taxonomies, one option could be getting the term you want to replace (as you're doing now), remove it with wp_remove_object_terms(), then use wp_set_post_terms() to append the new, modified term.
CodePudding user response:
Now I tried it like that
wp_remove_object_terms($products[0] ->ID, 'pa_pw', true);
wp_set_post_terms($products[0] ->ID, $random_number, 'pa_pw', true);
$terms = wp_get_post_terms($products[0] ->ID, 'pa_pw', true);
That seemed to work ONCE, and now it stopped working.
Update: It just added more and more values every time. It returned just the first value - which was the same every time after the first run