0

I have a search that works fine with custom fields, and one term per taxonomy. However I want to select multiple terms from one taxonomy "property-type".

My current query looks like ---

/?s=Search&property_city=Any+City&property_state=Any+State&address=&beds=Any&baths=Any&min_price=Any&max_price=Any&property_type=condo&property_type=duplex&min_area=Any&max_area=Any&post_type=property&hide-reports=list-property&property-contract-type=for-sale

I need this to use OR operator when selecting multiple terms --

 property_type=condo&property_type=duplex

Like so -

property_type=condo,duplex

Currently in my theme's functions --

 $s = $_GET['s'];
                 $meta_query=array();
            if ($s == __('Search', 'framework')) {
                $query->set('s', '');
            }
              $query->set('post_type', 'property');
              $query->set('post_status','publish');
              if (!empty($property_type)) {
               $query->set('property-type', $property_type);
                }
                if (!empty($property_city)) {
               $query->set('city-type',$property_city);
                }
if (!empty($property_neighborhood)) {
               $query->set('neighborhood',$property_neighborhood);
                }
              if (!empty($property_contract_type)) {
                 $query->set('property-contract-type', $property_contract_type);
                }

Full Code - https://pastebin.com/LC96j4v5

On my form -

<select name="property_type" class="form-control" multiple>
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
 $count = count($terms);
 if ( $count > 0  ){
echo "<option class='button' value='Any'>All</option>";
     foreach ( $terms as $term ) {
         echo "<option class='button' value='" . $term->slug . "'>" . $term->name . "</option>";
     }
 }
?>
  </select>
2
+50

You need to make only one change in the form, change the first line to:

<select name="property_type[]" class="form-control" multiple>

Name of the field has to include [] to be considered as an array when it arrives on the server side. In the query string it will be listed as:

property_type[]=condo&property_type[]=duplex

And, on server side, when you read this use this:

$property_type = (array)$_GET['property_type'];

This can go right at the top just after the $s = $_GET['s']; line,

Make sure to sanitize all the input data before using. More on data sanitizing you can find here: Validating Sanitizing and Escaping User Data

  • Hi, thanks for the reply. How ever, Im not sure where to apply the array to my code. – Rich Oct 18 '17 at 18:40
  • I have expanded my answer. Also, you have to have someplace where you set all the other variables for $property_city and everything else. Your code doesn't show that. – Milan Petrovic Oct 18 '17 at 19:12
  • Im not sure what do you mean? As far as setting the keys to the fields? – Rich Oct 18 '17 at 19:38
  • So far I have your code working, I didnt validate or nothing though. – Rich Oct 18 '17 at 19:50
0

You do not need to make any change to the form. You need to make a change in the theme's function to include a tax_query which will get multiple terms from your taxonomy property_type with a relation OR

 $s = $_GET['s'];
foreach($_GET as $key => $value){
   if($key == 'property_type'){
    $item_property_type['taxonomy'] = htmlspecialchars($key);
    $item_property_type['terms'] = htmlspecialchars($value);
    $item_property_type['field'] = 'slug';
    $list[] = $item_property_type;
  }
}
$taxproperty_type = array_merge(array('relation' => 'OR'), $list);

                 $meta_query=array();
            if ($s == __('Search', 'framework')) {
                $query->set('s', '');
            }
              $query->set('post_type', 'property');
              $query->set('post_status','publish');
              if (!empty($property_type)) {
               $query->set('tax_query',$taxproperty_type);
                }
                if (!empty($property_city)) {
               $query->set('city-type',$property_city);
                }
if (!empty($property_neighborhood)) {
               $query->set('neighborhood',$property_neighborhood);
                }
              if (!empty($property_contract_type)) {
                 $query->set('property-contract-type', $property_contract_type);
                }
  • Hi, thanks for the answer but it's now not respecting my query at all and when I search it shows all posts when I select any options. If i don't select anything from my drop down then it shows me the correct posts for the query. – Rich Oct 18 '17 at 18:40
  • I updated my question with the full code from my functions file. – Rich Oct 18 '17 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.