Advertisement
Guest User

WP SEARCH FILTER

a guest
Oct 18th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.62 KB | None | 0 0
  1. /* Search Filter for property
  2.       ================================= */  
  3. if(!function_exists('imic_search_filter')){
  4.     function imic_search_filter($query) {
  5.         if($query->is_search()) {
  6.             $property_contract_type=$property_report=$property_type=$property_neighborhood=$property_state_value=$property_city=$beds=$baths=$min_price=$max_price=$min_area=$max_area=$value_rating=$apor='';
  7.             $property_contract_type = isset($_GET['property_contract_type'])?$_GET['property_contract_type']:"";
  8.             $property_contract_type = ($property_contract_type == __('Contract', 'framework')) ? '' :$property_contract_type;
  9.             $property_type = isset($_GET['property_type'])?$_GET['property_type']:'';
  10.             $property_type = ($property_type == __('Any', 'framework')) ? '' :$property_type;
  11.             $property_report = isset($_GET['hide-reports'])?$_GET['hide-reports']:'';
  12.             $property_report = ($property_report == __('Any', 'framework')) ? '' :$property_report;
  13. $property_neighborhood = isset($_GET['neighborhood'])?$_GET['neighborhood']:'';
  14.             $property_neighborhood = ($property_neighborhood == __('Neighborhood', 'framework')) ? '' :$property_neighborhood;
  15.             $property_state_value = isset($_GET['property_state'])?$_GET['property_state']:'';
  16.             $property_state_value = ($property_state_value == __('Any State', 'framework')) ? '' : $property_state_value;
  17.             $property_city = isset($_GET['property_city'])?$_GET['property_city']:'';
  18.             $property_city = ($property_city == __('Any City', 'framework')) ? '' :$property_city;
  19.             $beds = isset($_GET['beds'])?$_GET['beds']:'';
  20.             $beds = ($beds == __('Any', 'framework')) ? '' : $beds;
  21.             $baths = isset($_GET['baths'])?$_GET['baths']:'';
  22.             $baths = ($baths == __('Any', 'framework')) ? '' : $baths;
  23.             $min_price = isset($_GET['min_price'])?$_GET['min_price']:'';
  24.             $min_price = ($min_price == __('Any', 'framework')) ? '' :$min_price;
  25.             $max_price = isset($_GET['max_price'])?$_GET['max_price']:'';
  26.             $max_price = ($max_price == __('Any', 'framework')) ? '' :$max_price;
  27.             $min_area = isset($_GET['min_area'])?$_GET['min_area']:'';
  28.             $min_area = ($min_area == __('Any', 'framework')) ? '' :$min_area;
  29.             $max_area = isset($_GET['max_area'])?$_GET['max_area']:'';
  30.             $max_area = ($max_area == __('Any', 'framework')) ? '' :$max_area;
  31. // Agent Property Value and Ratings
  32.  $value_rating = isset($_GET['vr'])?$_GET['vr']:'';
  33.             $value_rating = ($value_rating == __('Any', 'framework')) ? '' :$value_rating;
  34.  $apor = isset($_GET['rating_r'])?$_GET['rating_r']:'';
  35.             $apor = ($apor == __('Any', 'framework')) ? '' :$apor;
  36.            $id = isset($_GET['id'])?$_GET['id']:'';
  37.             $pincode = isset($_GET['pincode'])?$_GET['pincode']:'';
  38.             $address = isset($_GET['address'])?$_GET['address']:'';
  39.             // If the default text is in the box
  40.             if (!empty($property_contract_type)||!empty($property_report)||!empty($property_type)|| !empty($property_state_value) || !empty($baths) ||!empty($beds)||(!empty($min_price)||!empty($max_price))||(!empty($min_area)||!empty($max_area))||!empty($value_rating)||!empty($apor)||!empty($id)||!empty($pincode)||!empty($address)||!empty($property_city)||!empty($property_neighborhood)) {
  41.                  $s = $_GET['s'];
  42. foreach($_GET as $key => $value){
  43.    if($key == 'property_type'){
  44.     $item_property_type['taxonomy'] = htmlspecialchars($key);
  45.     $item_property_type['terms'] = htmlspecialchars($value);
  46.     $item_property_type['field'] = 'slug';
  47.     $list[] = $item_property_type;
  48.   }
  49. }
  50. $taxproperty_type = array_merge(array('relation' => 'OR'), $list);
  51.  
  52.                  $meta_query=array();
  53.             if ($s == __('Search', 'framework')) {
  54.                 $query->set('s', '');
  55.             }
  56.               $query->set('post_type', 'property');
  57.               $query->set('post_status','publish');
  58.   if (!empty($property_type)) {
  59.                $query->set('tax_query',$taxproperty_type);
  60.                 }
  61.                 if (!empty($property_city)) {
  62.                $query->set('city-type',$property_city);
  63.                 }
  64. if (!empty($property_neighborhood)) {
  65.                $query->set('neighborhood',$property_neighborhood);
  66.                 }
  67.               if (!empty($property_contract_type)) {
  68.                  $query->set('property-contract-type', $property_contract_type);
  69.                 }
  70.  
  71. if (!empty($property_report)) {
  72.                  $query->set('hide-reports', $property_report);
  73.                 }
  74.  
  75.              if (!empty($baths)) {
  76.                      array_push($meta_query, array(
  77.                             'key' => 'imic_property_baths',
  78.                             'value' => $baths,
  79.                              'type' => 'numeric',
  80.                             'compare' => '>='
  81.                         ));
  82.                 }
  83.                 if (!empty($beds)) {
  84.                     array_push($meta_query,array(
  85.                             'key' => 'imic_property_beds',
  86.                             'value' => $beds,
  87.                             'type' => 'numeric',
  88.                             'compare' => '>='
  89.                         ));
  90.                 }
  91.             if(!empty($min_price)&&!empty($max_price)){
  92.              array_push($meta_query,array(
  93.          'key' =>'imic_property_price',
  94.              'value'=>array($min_price,$max_price),
  95.              'type' =>'numeric',
  96.          'compare'=> 'BETWEEN'
  97.             ));
  98.             }
  99.             else{
  100.                if(!empty($min_price)){
  101.              array_push($meta_query,array(
  102.          'key' =>'imic_property_price',
  103.              'value'=>$min_price,
  104.              'type' =>'numeric',
  105.          'compare'=> '>='
  106.             ));
  107.             }
  108.             if(!empty($max_price)){
  109.              array_push($meta_query,array(
  110.          'key' =>'imic_property_price',
  111.              'value'=>$max_price,
  112.              'type' =>'numeric',
  113.          'compare'=> '<='
  114.             ));
  115.             }
  116.             }if(!empty($min_area)&&!empty($max_area)){
  117.               array_push($meta_query,array(
  118.          'key' => 'imic_property_area',
  119.              'value' => array($min_area,$max_area),
  120.              'type' => 'numeric',
  121.          'compare' => 'BETWEEN'
  122.             ));
  123.             }
  124.             else{
  125.                 if(!empty($min_area)){
  126.                 array_push($meta_query,array(
  127.          'key' => 'imic_property_area',
  128.              'value' => $min_area,
  129.              'type' => 'numeric',
  130.          'compare' => '>='
  131.             ));
  132.             }
  133.             if(!empty($max_area)){
  134.                 array_push($meta_query,array(
  135.          'key' => 'imic_property_area',
  136.              'value' => $max_area,
  137.              'type' => 'numeric',
  138.          'compare' => '<='
  139.             ));
  140.             }
  141.             }
  142.              if (!empty($property_state_value)) {
  143.                  array_push($meta_query,array(
  144.                 'key' => 'property_state',
  145.                 'value' => $property_state_value
  146.             ));
  147.             }
  148.  if (!empty($value_rating)) {
  149.                  array_push($meta_query,array(
  150.                 'key' => 'vr',
  151.                 'value' => $value_rating
  152.             ));
  153. }
  154.  if (!empty($apor)) {
  155.                  array_push($meta_query,array(
  156.                 'key' => 'rating_r',
  157.                 'value' => $apor,
  158.             ));      
  159.             }
  160.             if (!empty($id)) {
  161.               array_push($meta_query,array(
  162.                 'key' => 'imic_property_site_id',
  163.                 'value' => $id,
  164.                  'compare'=>'LIKE'
  165.             ));
  166.              }
  167.              if (!empty($pincode)) {
  168.                 array_push($meta_query,array(
  169.                 'key' => 'imic_property_pincode',
  170.                 'value' => $pincode
  171.             ));
  172.             }
  173.               if (!empty($address)) {
  174.                 array_push($meta_query,array(
  175.                 'key' => 'imic_property_site_address',
  176.                 'value' => $address,
  177.                 'compare' => 'LIKE',
  178.             ));
  179.             }
  180.              
  181.             $query->set('meta_query',$meta_query);
  182.             }else {
  183.                $s = $_GET['s'];
  184.                  if ($s == __('Search', 'framework')) {
  185.                       $query->set('s', '');
  186.                 $query->set('post_type', 'property');
  187.             }else{
  188.              $query->set('post_type', 'property');    
  189.             } }
  190.            
  191.             }
  192.           return $query;
  193.     }
  194. # Add Filters
  195. if(!is_admin()) {
  196.    add_filter('pre_get_posts', 'imic_search_filter'); }
  197.        }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement