What i'm trying to do is to programatically set woocommerce product category.
What I have is the term name test & sample
and the post id 9
, so to set the products category I have used get_term_by
and wp_set_object_terms
$name = 'test & sample';
$posid = 9;
//get the term based on name
$term = get_term_by('name', $name, 'product_cat');
//set woocommerce product category
wp_set_object_terms($posid, $term->term_id, 'product_cat');
As you can see my problem is the unsanitized value of $name
. What I have done so far is replace &
with &
which work.
$name = str_replace('&', '&', 'test & sample');
$posid = 9;
//get the term based on name
$term = get_term_by('name', $name, 'product_cat');
//set woocommerce product category
wp_set_object_terms($posid, $term->term_id, 'product_cat');
My question is how to Get term by name using unsanitized name value or how to sanitize the name value to get the term id properly.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire