Function Reference: geodir_default_location_where

Summary

Adds the default location filter to the where clause.

Global Values

$wp_query
(object) (required) WordPress Query object.

Default: None
$wpdb
(object) (required) WordPress Database object.

Default: None
$plugin_prefix
(string) (required) Geodirectory plugin table prefix.

Default: None
$wp
(object) (required) WordPress object.

Default: None

Package

GeoDirectory_Location_Manager

Parameters

$where
(string) (required) The WHERE clause of the query.

Default: None
$p_table
(string) (required) Post table.

Default: None

Return Values

(mixed|string)
  • Filterd where clause.

Change Log

Since: 1.0.0

Filters

‘geodir_location_allowed_location_where’ [Line: 1534]

Source File

geodir_default_location_where() is located in geodir_location_manager/geodir_location_hooks_actions.php [Line: 1531]

Source Code

function geodir_default_location_where( $where, $p_table = '' ) {
	global $wp_query, $wpdb, $table, $wp, $plugin_prefix;
	
	$allowed_location = apply_filters( 'geodir_location_allowed_location_where', true, $wp->query_vars, $table, $wp_query, $p_table );
	if ( !$allowed_location ) {
		return $where;
	}
	
	// my location set start
	if(isset($_SESSION['all_near_me'])){
		
	$mylat = $_SESSION['user_lat'];
	$mylon = $_SESSION['user_lon'];
	
	if(isset($_SESSION['near_me_range']) && is_numeric($_SESSION['near_me_range'])){$dist =$_SESSION['near_me_range']; }
	elseif(get_option('geodir_near_me_dist')!=''){$dist = get_option('geodir_near_me_dist');}
	else{ $dist = '200';  }
	
	$lon1 = $mylon- $dist/abs(cos(deg2rad($mylat))*69); 
	$lon2 = $mylon+$dist/abs(cos(deg2rad($mylat))*69);
	$lat1 = $mylat-($dist/69);
	$lat2 = $mylat+($dist/69);	
	
	$rlon1 = is_numeric(min($lon1,$lon2)) ? min($lon1,$lon2) : '';
	$rlon2 = is_numeric(max($lon1,$lon2)) ? max($lon1,$lon2) : '';
	$rlat1 = is_numeric(min($lat1,$lat2)) ? min($lat1,$lat2) : '';
	$rlat2 = is_numeric(max($lat1,$lat2)) ? max($lat1,$lat2) : '';
	
	$where .= " AND post_latitude between $rlat1 and $rlat2 
	AND post_longitude between $rlon1 and $rlon2 ";
	return $where;
	}	
	
	$where = str_replace( "0 = 1", "1=1", $where );
	$country = '';
	$region = '';
	$city = '';
	$neighbourhood = '';
	
	if ( isset( $_SESSION['gd_country'] ) && $_SESSION['gd_country'] != '' ) {
		$country = $_SESSION['gd_country'];
	}
	
	if ( $country == '' ) {
		// check if we have country  in query vars
		if ( isset( $wp->query_vars['gd_country'] ) && $wp->query_vars['gd_country'] != '' ) {
			$country = $wp->query_vars['gd_country'];
		}
	}
	
	if ( isset( $_SESSION['gd_region'] ) && $_SESSION['gd_region'] != '' ) {
		$region = $_SESSION['gd_region'];
	}
	
	if ( $region == '' ) {
		// check if we have region in query vars
		if ( isset( $wp->query_vars['gd_region'] ) && $wp->query_vars['gd_region'] != '' ) {
			$region = $wp->query_vars['gd_region'];
		}
	}
		
	if ( isset( $_SESSION['gd_city'] ) && $_SESSION['gd_city'] != '' ) {
		$city = $_SESSION['gd_city'];
	}
	
	if ( $city == '' ) {
		// check if we have city in query vars
		if ( isset($wp->query_vars['gd_city'] ) && $wp->query_vars['gd_city'] != '' ) {
			$city = $wp->query_vars['gd_city'];
		}
	}
	
	
	$neighbourhood = get_query_var( 'gd_neighbourhood' );
	if ( empty( $neighbourhood ) ) {	
		if ( isset( $wp->query_vars['gd_neighbourhood'] ) && $wp->query_vars['gd_neighbourhood'] != '' ) {
			$neighbourhood = $wp->query_vars['gd_neighbourhood'];
		}
	}	
	
	// added for map calls
	if ( empty( $neighbourhood ) ) {	
		if ( isset( $_REQUEST['gd_neighbourhood'] ) && $_REQUEST['gd_neighbourhood'] != '' ) {
			$neighbourhood = $_REQUEST['gd_neighbourhood'];
					if ( isset( $_REQUEST['gd_posttype'] ) && $_REQUEST['gd_posttype'] != '' ) {
						$p_table = "pd";
					}
		}
	}
	
	$format = "''";
	if ( is_array( $neighbourhood ) && !empty( $neighbourhood ) ) {
		$neighbourhood_length = count( $neighbourhood );
		$format = array_fill( 0, $neighbourhood_length, '%s' );
		$format = implode( ',', $format );
		
	} else if( !is_array( $neighbourhood ) && !empty( $neighbourhood ) ) {
		$format = "%s";
		$neighbourhood = array( $neighbourhood );
	}
		
	if ( $country != '' ) {
		//$where .= " AND FIND_IN_SET('[".$country."]', post_locations) ";
		$where .= " AND post_locations LIKE '%,[".$country."]' ";
	}
	
	if ( $region != '' ) {
		//$where .= " AND FIND_IN_SET('[".$region."]', post_locations) ";
		$where .= " AND post_locations LIKE '%,[".$region."],%' ";
	}
	
	if ( $city != '' ) {
		//$where .= " AND FIND_IN_SET('[".$city."]', post_locations) ";
		$where .= " AND post_locations LIKE '[".$city."],%' ";
	}

	if ( $neighbourhood != '' ) {
		$post_table = $table != '' ? $table . '.' : ''; /* fixed db error when $table is not set */
		if(!empty($p_table)){$post_table = $p_table. '.';}
		$where .= $wpdb->prepare( " AND " . $post_table . "post_neighbourhood IN ($format) " , $neighbourhood );
	}

	return $where;
}