I have a wordpress site with a search form that searches for messages depending on the choice of the form field, for custom fields, etc. This works fine, however on the search results page I have an exact copy of the form, except that I'm trying to pre-select the form based on the search string / url.
I use the conventional selection drop-down menu, and I set it to βmultipleβ so that it can use multiboot bootstraps with checkboxes. I asked a similar question HERE , but it was for checkboxes, and even though checkboxes are used in the multiselect bot, I still have to work with the dropdown list first.
So, after several attempts, I came close, but ran into several problems. In the code below, I took notes to explain again what I mean.
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
if (isset($_GET['property_type'])) {
foreach ($_GET['property_type'] as $proptypes) {
$selected .= ($proptypes === $term->slug) ? "selected" : "";
}
}
echo "<option value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>";
}
}
?>
</select>
source
share