Function Reference: searching_filter_where

Summary

Listing search where filter.

Global Values

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

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

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

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

Default: None

Package

GeoDirectory

Parameters

$where
(string) (required) The where query string.

Default: None

Return Values

(string)
  • Modified where query string.

Change Log

Since: 1.0.0

1.5.4 Modified to fix unable to find listings for a particular keyword in the content.

Filters

‘geodir_search_content_where’ [Line: 816]

‘geodir_search_terms_where’ [Line: 824]

Source File

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

Source Code

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

    $search_term = 'OR';
    $search_term = 'AND';
    $geodir_custom_search = '';
    $category_search_range = '';

    if (is_single() && get_query_var('post_type')) {
		return $where;
	}

    if (is_tax()) {
		return $where;
	}

	$s = trim($s);
    $s  = wp_specialchars_decode($s ,ENT_QUOTES);
    $s_A = wp_specialchars_decode($s_A ,ENT_QUOTES);

    $where = '';
    $better_search_terms = '';
    if (isset($_REQUEST['stype']))
        $post_types = esc_attr(wp_strip_all_tags($_REQUEST['stype']));
    else
        $post_types = 'gd_place';

    if ($s != '') {
        $keywords = explode(" ", $s);
        if(is_array($keywords) && $klimit = get_option('geodir_search_word_limit')){
            foreach($keywords as $kkey=>$kword){
                if(geodir_utf8_strlen($kword)<=$klimit){
                    unset($keywords[$kkey]);
                }
            }
        }

        if (!empty($keywords)) {
            foreach ($keywords as $keyword) {
                $keyword = trim($keyword);
                $keyword  = wp_specialchars_decode($keyword ,ENT_QUOTES);
                if ($keyword != '') {
                    /**
                     * Filter the search query keywords SQL.
                     *
                     * @since 1.5.9
                     * @package GeoDirectory
                     * @param string $better_search_terms The query values, default: `' OR ( ' . $wpdb->posts . '.post_title LIKE "' . $keyword . '" OR ' . $wpdb->posts . '.post_title LIKE "' . $keyword . '%" OR ' . $wpdb->posts . '.post_title LIKE "% ' . $keyword . '%" )'`.
                     * @param array $keywords The array of keywords for the query.
                     * @param string $keyword The single keyword being searched.
                     */
					$better_search_terms .= apply_filters("geodir_search_better_search_terms",' OR ( ' . $wpdb->posts . '.post_title LIKE "' . $keyword . '" OR ' . $wpdb->posts . '.post_title LIKE "' . $keyword . '%" OR ' . $wpdb->posts . '.post_title LIKE "% ' . $keyword . '%" )',$keywords,$keyword);
                }
            }
        }
    }

    /* get taxonomy */
    $taxonomies = geodir_get_taxonomies($post_types, true);
    if($taxonomies) {
        $taxonomies = implode("','", $taxonomies);
        $taxonomies = "'" . $taxonomies . "'";
    }else{$taxonomies='';}

    $content_where = $terms_where = '';
	if ($s != '') {
        /**
         * Filter the search query content where values.
         *
         * @since 1.5.0
         * @package GeoDirectory
         * @param string $content_where The query values, default: `" OR ($wpdb->posts.post_content LIKE \"$s\" OR $wpdb->posts.post_content LIKE \"$s%\" OR $wpdb->posts.post_content LIKE \"% $s%\" OR $wpdb->posts.post_content LIKE \"%>$s%\" OR $wpdb->posts.post_content LIKE \"%\n$s%\") ") "`.
         */
		$content_where = apply_filters("geodir_search_content_where"," OR ($wpdb->posts.post_content LIKE \"$s\" OR $wpdb->posts.post_content LIKE \"$s%\" OR $wpdb->posts.post_content LIKE \"% $s%\" OR $wpdb->posts.post_content LIKE \"%>$s%\" OR $wpdb->posts.post_content LIKE \"%\n$s%\") ");
        /**
         * Filter the search query term values.
         *
         * @since 1.5.0
         * @package GeoDirectory
         * @param string $terms_where The separator, default: `" AND ($wpdb->terms.name LIKE \"$s\" OR $wpdb->terms.name LIKE \"$s%\" OR $wpdb->terms.name LIKE \"% $s%\" OR $wpdb->terms.name IN ($s_A)) "`.
         */
        $terms_where = apply_filters("geodir_search_terms_where"," AND ($wpdb->terms.name LIKE \"$s\" OR $wpdb->terms.name LIKE \"$s%\" OR $wpdb->terms.name LIKE \"% $s%\" OR $wpdb->terms.name IN ($s_A)) ");
	}


    // get term sql
    $term_sql = "SELECT $wpdb->term_taxonomy.term_id, $wpdb->terms.name, $wpdb->term_taxonomy.taxonomy
                    FROM $wpdb->term_taxonomy,  $wpdb->terms, $wpdb->term_relationships
                    WHERE $wpdb->term_taxonomy.term_id =  $wpdb->terms.term_id
                    AND $wpdb->term_relationships.term_taxonomy_id =  $wpdb->term_taxonomy.term_taxonomy_id
                    AND $wpdb->term_taxonomy.taxonomy in ( {$taxonomies} )
                    $terms_where
                    GROUP BY $wpdb->term_taxonomy.term_id";

    $term_results = $wpdb->get_results($term_sql);
    $term_ids = array();
    $terms_sql = '';

    if(!empty($term_results)){
        foreach($term_results as $term_id){
            $term_ids[] = $term_id;
        }
        if (!empty($term_ids)) {
            foreach($term_ids as $term){
                if ($term->taxonomy == $post_types.'_tags') {
                    $terms_sql .= $wpdb->prepare(" OR FIND_IN_SET(%s , " . $table . ".post_tags) ", $term->name);
                } else {
                    $terms_sql .= " OR FIND_IN_SET($term->term_id , " . $table . "." . $post_types . "category) ";
                }
            }
        }
    }


    if ($snear != '') {


        if (is_numeric($gd_session->get('near_me_range')) && !isset($_REQUEST['sdist'])) {
            $dist = $gd_session->get('near_me_range');
        }
        $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 ( ( $wpdb->posts.post_title LIKE \"$s\" $better_search_terms)
			                    $content_where 
								$terms_sql 
							)
						AND $wpdb->posts.post_type in ('{$post_types}')
						AND ($wpdb->posts.post_status = 'publish')
						AND ( " . $table . ".post_latitude between $rlat1 and $rlat2 )
						AND ( " . $table . ".post_longitude between $rlon1 and $rlon2 ) ";

        if (isset($_REQUEST['sdist']) && $_REQUEST['sdist'] != 'all') {
            $DistanceRadius = geodir_getDistanceRadius(get_option('geodir_search_dist_1'));
            $where .= " AND CONVERT((" . $DistanceRadius . " * 2 * ASIN(SQRT( POWER(SIN((ABS($mylat) - ABS(" . $table . ".post_latitude)) * pi()/180 / 2), 2) +COS(ABS($mylat) * pi()/180) * COS( ABS(" . $table . ".post_latitude) * pi()/180) *POWER(SIN(($mylon - " . $table . ".post_longitude) * pi()/180 / 2), 2) ))),DECIMAL(64,4)) <= " . $dist;
        }

    } else {



        $where .= " AND (	( $wpdb->posts.post_title LIKE \"$s\" $better_search_terms)
                            $content_where  
							$terms_sql 
					    ) 
					
				AND $wpdb->posts.post_type in ('$post_types')
				AND ($wpdb->posts.post_status = 'publish') ";
    }

	########### WPML ###########
    if ( geodir_wpml_is_post_type_translated( $post_types ) ) {
		$lang_code = ICL_LANGUAGE_CODE;

		if ($lang_code && $post_types) {
            $where .= " AND icl_t.language_code = '".$lang_code."' AND icl_t.element_type IN('post_" . $post_types . "') ";
        }
    }
    ########### WPML ###########

    return $where;
}