Function Reference: geodir_search_widget_location_filter_fields

Summary

Set distance field in widget listing query fields search for location filter.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None
$gd_query_args_widgets
(array) (required) Widget query args.

Default: None
$snear
(string) (required) Nearest location to search.

Default: None
$gd_session
(object) (required) GeoDirectory Session object.

Default: None

Parameters

$fields
(string) (required) Fields SQL.

Default: None
$table
(string) (required) Table name.

Default: None
$post_type
(string) (required) Post type.

Default: None

Return Values

(string)
  • Modified fields SQL.

Change Log

Since: 1.6.22

Source File

geodir_search_widget_location_filter_fields() is located in geodirectory-functions/listing_filters.php [Line: 1092]

Source Code

function geodir_search_widget_location_filter_fields( $fields, $table, $post_type ) {
    global $wpdb, $gd_query_args_widgets, $snear, $gd_session;

    if ( !empty( $gd_query_args_widgets['gd_location'] ) && geodir_is_page( 'search' ) && !empty( $_REQUEST['sgeo_lat'] ) && !empty( $_REQUEST['sgeo_lon'] ) ) {
        $location_allowed = function_exists( 'geodir_cpt_no_location' ) && geodir_cpt_no_location( $post_type ) ? false : true;

        if ( $location_allowed && strpos( strtolower( $fields ), ' as distance ' ) === false && ( $snear != '' || $gd_session->get( 'all_near_me' ) ) ) {
            $latitude = sanitize_text_field( $_REQUEST['sgeo_lat'] );
            $longitude = sanitize_text_field( $_REQUEST['sgeo_lon'] );
            $radius = geodir_getDistanceRadius( get_option( 'geodir_search_dist_1' ) );

            $fields .= $wpdb->prepare( ", (" . $radius . " * 2 * ASIN(SQRT(POWER(SIN((ABS(%s) - ABS(" . $table . ".post_latitude)) * PI() / 180 / 2), 2) + COS(ABS(%s) * PI() / 180) * COS(ABS(" . $table . ".post_latitude) * PI() / 180) * POWER(SIN((%s - " . $table . ".post_longitude) * PI() / 180 / 2), 2)))) AS distance ", $latitude, $latitude, $longitude );
        }
    }

    return $fields;
}