Function Reference: geodir_advance_search_where

Summary

This function has not been documented yet.

Filters

‘advance_search_where_query’ [Line: 345]

Source File

geodir_advance_search_where() is located in geodir_advance_search_filters/geodirectory_advance_search_function.php [Line: 182]

Source Code

function geodir_advance_search_where( $where ) {  
	global $wpdb, $geodir_post_type, $table, $plugin_prefix, $dist, $mylat, $mylon, $s, $snear, $s, $s_A, $s_SA, $search_term;

	if( isset( $_REQUEST['stype'] ) ) {
		$post_types = esc_attr($_REQUEST['stype']);
	} else {
		$post_types = 'gd_place';
	}
	
	/* check for post type other then geodir post types */
	if( !geodir_is_geodir_search( $where ) ) {
		return $where;
	}
	
	/* Add categories filters */
	$category_filter = false;	
	$category_search_query = '';
	$geodir_custom_search = '';
	$category_search_range = ''; 

	$sql = $wpdb->prepare( "SELECT * FROM " . GEODIR_ADVANCE_SEARCH_TABLE . " WHERE post_type = %s ORDER BY sort_order", array( $post_types ) );
	$taxonomies = $wpdb->get_results( $sql );

	if( !empty( $taxonomies ) ) {
		foreach( $taxonomies as $taxonomy_obj ) {
			$taxonomy_obj = stripslashes_deep($taxonomy_obj); // strip slashes
			switch( $taxonomy_obj->field_input_type ) {
				case 'RANGE':
					// SEARCHING BY RANGE FILTER 
					switch( $taxonomy_obj->search_condition ) {
						case 'SINGLE':
							$value = esc_attr($_REQUEST['s' . $taxonomy_obj->site_htmlvar_name]);
							
							if( !empty( $value ) ) {
								$category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." = $value) "; 
							}
						break;
						
						case 'FROM':
							$minvalue = @esc_attr($_REQUEST['smin'.$taxonomy_obj->site_htmlvar_name]);
							$smaxvalue = @esc_attr($_REQUEST['smax'.$taxonomy_obj->site_htmlvar_name]);
							
							if( !empty( $minvalue ) ) { 
								$category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." >= '".$minvalue."') "; 
							}
								
							if( !empty( $smaxvalue ) ) {
								$category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." <= '".$smaxvalue."') ";
							}			
						break;
						
						case 'RADIO':
							// This code in main geodirectory listing filter 
						break;
						
						default :
							if( isset( $_REQUEST['s'.$taxonomy_obj->site_htmlvar_name] ) && $_REQUEST['s'.$taxonomy_obj->site_htmlvar_name] != '' ) {
								$serchlist =  explode( "-", esc_attr($_REQUEST['s'.$taxonomy_obj->site_htmlvar_name]) );
								$first_value  = @$serchlist[0];//100 200
								$second_value = @trim( $serchlist[1], ' ' );
								$rest = substr( $second_value, 0, 4 ); 
								 
								if( $rest == 'Less' ) {
									$category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." <= $first_value ) "; 
									
								} else if ( $rest == 'More' ) {
									$category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." >= $first_value) ";
									
								} else if( $second_value != '' ) {
									$category_search_range  .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." between $first_value and $second_value ) ";
								}
							}
						break;
					}
					// END SEARCHING BY RANGE FILTER  
				break;
				
				case 'DATE' :
					$single = '';
					$value = @esc_attr($_REQUEST['s'.$taxonomy_obj->site_htmlvar_name]);
					if(	isset( $value ) &&!empty( $value ) ) {
						$minvalue = $value;
						$maxvalue = '';
						$single = '1';
					} else {
						$minvalue = @esc_attr($_REQUEST['smin'.$taxonomy_obj->site_htmlvar_name]);
						$maxvalue = @esc_attr($_REQUEST['smax'.$taxonomy_obj->site_htmlvar_name]);
					}
				
					if( $taxonomy_obj->site_htmlvar_name == 'event' ) {
						$category_search_range .= " ";
					} else if( $taxonomy_obj->field_data_type == 'DATE' ) {
						$start_date = date( 'Y-m-d', strtotime( $minvalue ) );
						$start_end = date( 'Y-m-d', strtotime( $maxvalue ) );
						
						$minvalue = $wpdb->get_var( "SELECT UNIX_TIMESTAMP( STR_TO_DATE( '".$start_date."','%Y-%m-%d'))" );
						$maxvalue = $wpdb->get_var( "SELECT UNIX_TIMESTAMP( STR_TO_DATE( '".$start_end."','%Y-%m-%d'))" );
							
						if( $single == '1' ) {
							$category_search_range .= " AND ( unix_timestamp(".$table.'.'.$taxonomy_obj->site_htmlvar_name.") = '".$minvalue."' )";
						} else {
							if( !empty( $minvalue ) ) {
								$category_search_range .= " AND ( unix_timestamp(".$table.'.'.$taxonomy_obj->site_htmlvar_name.") >= '".$minvalue."' )";
							}
							if( !empty( $maxvalue ) ) {
								$category_search_range .= " AND ( unix_timestamp(".$table.'.'.$taxonomy_obj->site_htmlvar_name.") <= '".$maxvalue."' )";
							}
						}		
					} else if( $taxonomy_obj->field_data_type == 'TIME' ) {
						if( $single == '1' ) {
							 $category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." = '".$minvalue.":00' )";  
						} else {
							if( !empty( $minvalue ) ) {
								$category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." >= '".$minvalue.":00' )"; 
							}
							if( !empty( $maxvalue ) ) {
								$category_search_range .= " AND ( ".$table.'.'.$taxonomy_obj->site_htmlvar_name." <= '".$maxvalue.":00' )";
							}
						}
					}
				break;
				default:
					$category_search = ''; 
					if( isset( $_REQUEST['s'.$taxonomy_obj->site_htmlvar_name] ) && is_array( $_REQUEST['s'.$taxonomy_obj->site_htmlvar_name] ) ) {
						$i = 0;
						$add_operator = ''; 
						foreach( $_REQUEST['s'.$taxonomy_obj->site_htmlvar_name] as $val ) {
                            $val = esc_attr($val);
							if( $val != '' ) {
								if( $i != 0 ) {
									$add_operator = $search_term;
								}
								
								$category_search .= $add_operator." FIND_IN_SET('{$val}', ".$table.".".$taxonomy_obj->site_htmlvar_name." ) ";
								$i++; 
							} 
						}
						
						if( !empty( $category_search ) ) {
							$geodir_custom_search .= " AND (".$category_search.")";
						}
					} else if( isset($_REQUEST['s'.$taxonomy_obj->site_htmlvar_name] ) ) {
						$site_htmlvar_name = $taxonomy_obj->site_htmlvar_name;
							
						if( $site_htmlvar_name == 'post' ) {
							$site_htmlvar_name = $site_htmlvar_name.'_address';
						}
							
						if( $_REQUEST['s'.$taxonomy_obj->site_htmlvar_name] ) {
							$geodir_custom_search .= " AND ".$table.".".$site_htmlvar_name." LIKE '%".esc_attr($_REQUEST['s'.$taxonomy_obj->site_htmlvar_name])."%' ";
						}
					}
				break;
			}
		} 
	}
	if( !empty( $geodir_custom_search ) ) {
		$where .= $geodir_custom_search;
	}
	if( !empty( $category_search_range ) ) {
		$where .= $category_search_range;
	}
	
	$where =  apply_filters( 'advance_search_where_query', $where );
	
	return $where;
}