I have a front end form, I need that form to attach a taxonomy term to the post, however, I have offered users a dropdown of options, 'a dropdown of custom taxonomy terms.' To create this drop down i have tried a select attribute and pulled the terms through as options. The problem comes however, due to the fact I have a large multitude of taxonomies that are not practical to show in a dropdown. In this case I pulled the terms through a datalist. This then creates the problem that users can, with the code i'm using below, create their own custom taxonomy terms by writing something that isn't suggested by the datalist.
I need to stop this, either by stopping the datalist from allowing values that aren't in the options, or by using
wp_set_object_terms($post_id, $_POST['terms'], 'products-tax', true);
to not accept any taxonomy terms that do not exist, and parse an error to say choose a value value from the list. However, I cannot work out how to do either option.
Any help would be invaluable thank you!
Code I am using:
This is used to set the taxonomy and terms for the post.
'tax_input' => array('products-tax' => $_POST['terms'])
This attaches the terms
wp_set_object_terms($post_id, $_POST['terms'], 'products-tax', true);
This is the input and datalist that fetches the taxonomy terms and lists them as options.
<input type="text" list="products" id="product" name="terms[]" required>
<datalist id="products">
<?php
$products_tax = get_terms('products-tax', 'orderby=id&hide_empty=0');
$counter = 0;
foreach ($products_tax as $product ) {
$counter++;
echo '<option value="'.$product->slug.'">';
}
?>
</datalist>
select
too. Why would you use adatalist
for that?wp_set_object_terms($post_id, $_POST['terms'], 'products-tax', true);
causes the problem by allowing users to make their own taxonomy term, instead I would want it to parse an error to say 'this product doesn't exist, please choose an eligible product' sort of a thing.