php – WordPress wp_set_object_terms does not assign product to custom taxonomy
Question
I have create a custom taxonomy called regions
with acf. and have create also a select option on dokan multivendor dashboard where they can create new product
<?php
$regions = get_terms([
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'taxonomy' => 'region'
]);
?>
<label for="regions"><?php esc_html_e('Region', 'your-text-domain'); ?></label><br>
<input type="hidden" name="region_id" id="region_id" value="">
<select name="regions" class="dokan-form-control">
<option value=""><?php esc_html_e('Select Region', 'your-text-domain'); ?></option>
<?php foreach ($regions as $region) {
$parent_categories = get_terms([
'taxonomy' => 'region',
'hide_empty' => false,
'parent' => $region->term_id
]);
if (!empty($parent_categories)) { // check if there are any child categories
?>
<?php foreach ($parent_categories as $parent_category) {
$child_categories = get_terms([
'taxonomy' => 'region',
'hide_empty' => false,
'parent' => $parent_category->term_id
]);
if (!empty($child_categories)) { // check if there are any child categories
?>
<optgroup label="<?php echo $parent_category->name; ?>">
<?php foreach ($child_categories as $child_category) { ?>
<option value="<?php echo $child_category->term_taxonomy_id; ?>" id="in-region-<?php echo $child_category->term_taxonomy_id; ?>"> <?php echo $child_category->name; ?> </option>
<?php } ?>
</optgroup>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
</select>
and now i try to assign this post id when its created to regions like this
//path: dokan-lit/includes/Dashboard/Templates/Products.php
public function handle_product_add() {
if ( isset( $postdata['add_product'] ) ) {
$post_title = sanitize_text_field( $postdata['post_title'] );
$post_content = wp_kses_post( $postdata['post_content'] );
$post_excerpt = wp_kses_post( $postdata['post_excerpt'] );
$featured_image = absint( sanitize_text_field( $postdata['feat_image_id'] ) );
<!-- i have added this line here to get regions id -->
$regionId = sanitize_text_field($postdata['regions']);
$product_id = wp_insert_post( $post_data );
if ( $product_id ) {
// and trying to assign this product id to region id like this.
wp_set_object_terms($product_id, $regionId, 'region');
}
But i see this is not working. Any idea how to fix this problem?
0
3 weeks
2023-05-15T09:39:09-05:00
2023-05-15T09:39:09-05:00 0 Answers
0 views
0
Leave an answer