/**
  * Offers property search functionality
  *
  * @param $search_args  Array   search arguments
  * @return mixed    Array   modified search arguments
  */
 function inspiry_property_search($search_args)
 {
     /* taxonomy query and meta query arrays */
     $tax_query = array();
     $meta_query = array();
     /* Keyword Based Search */
     if (isset($_GET['keyword'])) {
         $keyword = trim($_GET['keyword']);
         if (!empty($keyword)) {
             $search_args['s'] = $keyword;
         }
     }
     /* property type taxonomy query */
     if (!empty($_GET['type']) && $_GET['type'] != 'any') {
         $tax_query[] = array('taxonomy' => 'property-type', 'field' => 'slug', 'terms' => $_GET['type']);
     }
     /* property location taxonomy query */
     $location_select_names = inspiry_get_location_select_names();
     $locations_count = count($location_select_names);
     for ($l = $locations_count - 1; $l >= 0; $l--) {
         if (isset($_GET[$location_select_names[$l]])) {
             $current_location = $_GET[$location_select_names[$l]];
             if (!empty($current_location) && $current_location != 'any') {
                 $tax_query[] = array('taxonomy' => 'property-city', 'field' => 'slug', 'terms' => $current_location);
                 break;
             }
         }
     }
     /* property feature taxonomy query */
     if (isset($_GET['features'])) {
         $required_features_slugs = $_GET['features'];
         if (is_array($required_features_slugs)) {
             $slugs_count = count($required_features_slugs);
             if ($slugs_count > 0) {
                 /* build an array of existing features slugs to validate required feature slugs */
                 $existing_features_slugs = array();
                 $existing_features = get_terms('property-feature', array('hide_empty' => false));
                 $existing_features_count = count($existing_features);
                 if ($existing_features_count > 0) {
                     foreach ($existing_features as $feature) {
                         $existing_features_slugs[] = $feature->slug;
                     }
                 }
                 foreach ($required_features_slugs as $feature_slug) {
                     if (in_array($feature_slug, $existing_features_slugs)) {
                         // validate slug
                         $tax_query[] = array('taxonomy' => 'property-feature', 'field' => 'slug', 'terms' => $feature_slug);
                     }
                 }
             }
         }
     }
     /* property status taxonomy query */
     if (!empty($_GET['status']) && $_GET['status'] != 'any') {
         $tax_query[] = array('taxonomy' => 'property-status', 'field' => 'slug', 'terms' => $_GET['status']);
     }
     /* Property Bedrooms Parameter */
     if (!empty($_GET['bedrooms']) && $_GET['bedrooms'] != 'any') {
         $meta_query[] = array('key' => 'REAL_HOMES_property_bedrooms', 'value' => $_GET['bedrooms'], 'compare' => '>=', 'type' => 'DECIMAL');
     }
     /* Property Bathrooms Parameter */
     if (!empty($_GET['bathrooms']) && $_GET['bathrooms'] != 'any') {
         $meta_query[] = array('key' => 'REAL_HOMES_property_bathrooms', 'value' => $_GET['bathrooms'], 'compare' => '>=', 'type' => 'DECIMAL');
     }
     /* Property ID Parameter */
     if (isset($_GET['property-id']) && !empty($_GET['property-id'])) {
         $property_id = trim($_GET['property-id']);
         $meta_query[] = array('key' => 'REAL_HOMES_property_id', 'value' => $property_id, 'compare' => 'LIKE', 'type' => 'CHAR');
     }
     /* Logic for Min and Max Price Parameters */
     if (isset($_GET['min-price']) && $_GET['min-price'] != 'any' && isset($_GET['max-price']) && $_GET['max-price'] != 'any') {
         $min_price = doubleval($_GET['min-price']);
         $max_price = doubleval($_GET['max-price']);
         if ($min_price >= 0 && $max_price > $min_price) {
             $meta_query[] = array('key' => 'REAL_HOMES_property_price', 'value' => array($min_price, $max_price), 'type' => 'NUMERIC', 'compare' => 'BETWEEN');
         }
     } elseif (isset($_GET['min-price']) && $_GET['min-price'] != 'any') {
         $min_price = doubleval($_GET['min-price']);
         if ($min_price > 0) {
             $meta_query[] = array('key' => 'REAL_HOMES_property_price', 'value' => $min_price, 'type' => 'NUMERIC', 'compare' => '>=');
         }
     } elseif (isset($_GET['max-price']) && $_GET['max-price'] != 'any') {
         $max_price = doubleval($_GET['max-price']);
         if ($max_price > 0) {
             $meta_query[] = array('key' => 'REAL_HOMES_property_price', 'value' => $max_price, 'type' => 'NUMERIC', 'compare' => '<=');
         }
     }
     /* Logic for Min and Max Area Parameters */
     if (isset($_GET['min-area']) && !empty($_GET['min-area']) && isset($_GET['max-area']) && !empty($_GET['max-area'])) {
         $min_area = intval($_GET['min-area']);
         $max_area = intval($_GET['max-area']);
         if ($min_area >= 0 && $max_area > $min_area) {
             $meta_query[] = array('key' => 'REAL_HOMES_property_size', 'value' => array($min_area, $max_area), 'type' => 'NUMERIC', 'compare' => 'BETWEEN');
         }
     } elseif (isset($_GET['min-area']) && !empty($_GET['min-area'])) {
         $min_area = intval($_GET['min-area']);
         if ($min_area > 0) {
             $meta_query[] = array('key' => 'REAL_HOMES_property_size', 'value' => $min_area, 'type' => 'NUMERIC', 'compare' => '>=');
         }
     } elseif (isset($_GET['max-area']) && !empty($_GET['max-area'])) {
         $max_area = intval($_GET['max-area']);
         if ($max_area > 0) {
             $meta_query[] = array('key' => 'REAL_HOMES_property_size', 'value' => $max_area, 'type' => 'NUMERIC', 'compare' => '<=');
         }
     }
     /* if more than one taxonomies exist then specify the relation */
     $tax_count = count($tax_query);
     if ($tax_count > 1) {
         $tax_query['relation'] = 'AND';
     }
     /* if more than one meta query elements exist then specify the relation */
     $meta_count = count($meta_query);
     if ($meta_count > 1) {
         $meta_query['relation'] = 'AND';
     }
     if ($tax_count > 0) {
         $search_args['tax_query'] = $tax_query;
     }
     /* if meta query has some values then add it to base home page query */
     if ($meta_count > 0) {
         $search_args['meta_query'] = $meta_query;
     }
     /* Sort By Price */
     if (isset($_GET['min-price']) && $_GET['min-price'] != 'any' || isset($_GET['max-price']) && $_GET['max-price'] != 'any') {
         $search_args['orderby'] = 'meta_value_num';
         $search_args['meta_key'] = 'REAL_HOMES_property_price';
         $search_args['order'] = 'ASC';
     }
     return $search_args;
 }
Example #2
0
<?php

global $inspiry_options;
// number of locations chosen from theme options
$location_select_count = inspiry_get_locations_number();
$location_select_names = inspiry_get_location_select_names();
$location_titles = inspiry_get_location_titles($location_select_count);
// override select boxes titles based on theme options data
for ($i = 1; $i <= $location_select_count; $i++) {
    $temp_location_title = $inspiry_options['inspiry_search_location_title_' . $i];
    if (!empty($temp_location_title)) {
        $location_titles[$i - 1] = $temp_location_title;
    }
}
// Generate required location select boxes
for ($i = 0; $i < $location_select_count; $i++) {
    ?>
    <div class="option-bar property-location">
        <select name="<?php 
    echo esc_attr($location_select_names[$i]);
    ?>
" id="<?php 
    echo esc_attr($location_select_names[$i]);
    ?>
" data-title="<?php 
    echo esc_attr($location_titles[$i]);
    ?>
" class="search-select">
            <option value="any"><?php 
    echo $location_titles[$i] . ' ' . __('(Any)', 'inspiry');
    ?>