• Resolved aravenwood

    (@aravenwood)


    I would like to disable the ability to enter wildcard searches of my directory – i.e. remove the ability to enter %piz% to return searches that include ‘pizza’, ‘pizzaz’, and ‘pizzicato’. Is there a setting within the plugin that controls this behavior, or way to override the search through a hook to control this?

    Any help appreciated in advance.

    Thanks,

    Michael

    P.S.: it appears the search feature on your support forum is broken and never returns any results for any search term entered.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Stiofan

    (@stiofansisland)

    Hi Michael,

    Please use this code snippet to do that:

    function gd_snippet_advanced_wildcard_search_title_where( $where, $s ) {
    	global $wpdb, $s, $geodir_post_type, $gd_exact_search;
    
    	if ( $s != '' && ! $gd_exact_search ) {
    		$table = geodir_db_cpt_table( $geodir_post_type );
    		$keywords = array_unique( explode( " ", $s ) );
    
    		if ( is_array( $keywords ) && ( $klimit = (int) geodir_get_option( 'search_word_limit' ) ) ) {
    			foreach ( $keywords as $kkey => $kword ) {
    				if ( geodir_utf8_strlen( $kword ) <= $klimit ) {
    					unset( $keywords[ $kkey ] );
    				}
    			}
    		}
    
    		$_where = array();
    
    		if ( ! empty( $keywords ) ) {
    			foreach ( $keywords as $keyword ) {
    				$keyword = stripslashes( wp_specialchars_decode( trim( $keyword ), ENT_QUOTES ) );
    
    				if ( $keyword != '' ) {
    					$parts = array();
    
    					$wild_cards_post_title = array( $keyword . ' %', '% ' . $keyword, '% ' . $keyword . ' %' );
    					foreach ( $wild_cards_post_title as $wild_card ) {
    						$parts[] = "{$wpdb->posts}.post_title LIKE %s";
    					}
    
    					$wild_cards_search_title = array( $keyword, $keyword . ' %', '% ' . $keyword, '% ' . $keyword . ' %' );
    					foreach ( $wild_cards_search_title as $wild_card ) {
    						$parts[] = "{$table}._search_title LIKE %s";
    					}
    
    					$_where[] = $wpdb->prepare( implode( " OR ", $parts ), array_merge( $wild_cards_post_title, $wild_cards_search_title ) );
    				}
    			}
    
    			if ( ! empty( $_where ) ) {
    				$where = " OR " . implode( " OR ", $_where );
    			}
    		}
    	}
    
    	return $where;
    }
    add_filter( 'geodir_search_better_search_terms_parts', 'gd_snippet_advanced_wildcard_search_title_where', 10, 2 );
    
    function gd_snippet_advanced_wildcard_search_content_where( $where ) {
    	global $wpdb, $s, $geodir_post_type, $gd_exact_search;
    
    	if ( $s != '' && ! $gd_exact_search ) {
    		$table = geodir_db_cpt_table( $geodir_post_type );
    		$keywords = array_unique( explode( " ", $s ) );
    
    		if ( is_array( $keywords ) && ( $klimit = (int) geodir_get_option( 'search_word_limit' ) ) ) {
    			foreach ( $keywords as $kkey => $kword ) {
    				if ( geodir_utf8_strlen( $kword ) <= $klimit ) {
    					unset( $keywords[ $kkey ] );
    				}
    			}
    		}
    
    		$_where = array();
    		if ( ! empty( $keywords ) ) {
    			foreach ( $keywords as $keyword ) {
    				$keyword = stripslashes( wp_specialchars_decode( trim( $keyword ), ENT_QUOTES ) );
    
    				if ( $keyword != '' ) {
    					$parts = array();
    					$wild_cards = array( $keyword, $keyword . ' %', $keyword . ',%', '% ' . $keyword, '% ' . $keyword . ' %' , '% ' . $keyword . ',%' , '% ' . $keyword . '.%' , '%,' . $keyword, '%,' . $keyword . ' %' , '%,' . $keyword . ',%' , '%,' . $keyword . '.%' );
    
    					foreach ( $wild_cards as $wild_card ) {
    						$parts[] = "$wpdb->posts.post_content LIKE %s";
    					}
    
    					$_where[] = $wpdb->prepare( '( ' . implode( " OR ", $parts ) . ' )', $wild_cards );
    				}
    			}
    
    			if ( $s == implode( " ", $keywords ) && count( $keywords ) == 1 ) {
    				$where = '';
    			}
    
    			$_where = count( $_where ) > 1 ? '( ' . implode( " AND ", $_where ) . ' )' : implode( " AND ", $_where );
    			$where .= ! empty( $_where ) ? " OR " . $_where . " " : '';
    		}
    	}
    
    	return $where;
    }
    add_filter( 'geodir_search_content_where', 'gd_snippet_advanced_wildcard_search_content_where', 99, 1 );

    Thanks,

    Stiofan

    Thread Starter aravenwood

    (@aravenwood)

    Thank you for the code Stiofan, I appreciate it.

    It turns out that since you responded my need has changed. Rather than remove wildcard behavior entirely, I would instead like for every search to be wildcard – but without requiring the user to enter the wildcard symbol (%) into the search box. In effect I am looking for a way to have the code inject the % wildcard onto every search – or, even better if possible, if there is a setting I missed somewhere in plugin which will do this.

    Thank you again!
    Michael

    Plugin Author Stiofan

    (@stiofansisland)

    Hello,

    Please use the below snippet:

    function gd_snippet_advanced_wildcard_search_title_where( $where, $s ) {
    	global $wpdb, $s, $geodir_post_type, $gd_exact_search;
    
    	if ( $s != '' && ! $gd_exact_search ) {
    		$table = geodir_db_cpt_table( $geodir_post_type );
    		$keywords = array_unique( explode( " ", $s ) );
    
    		if ( is_array( $keywords ) && ( $klimit = (int) geodir_get_option( 'search_word_limit' ) ) ) {
    			foreach ( $keywords as $kkey => $kword ) {
    				if ( geodir_utf8_strlen( $kword ) <= $klimit ) {
    					unset( $keywords[ $kkey ] );
    				}
    			}
    		}
    
    		if ( empty( $keywords ) ) {
    			$keywords = array( $s );
    		}
    
    		$the_post = $wpdb->get_row( "SELECT * FROM {$table} LIMIT 1" );
    		$the_post_fields = ! empty( $the_post ) ? array_keys( (array) $the_post ) : array();
    
    		$custom_fields = array( 'salle_departement' );
    		$search_fields = array();
    
    		foreach ( $custom_fields as $custom_field ) {
    			if ( ! empty( $the_post_fields ) && in_array( $custom_field, $the_post_fields ) ) {
    				$search_fields[] = $custom_field;
    			}
    		}
    
    		$_where = array();
    		if ( ! empty( $keywords ) ) {
    			if ( ! ( $s == implode( " ", $keywords ) && count( $keywords ) == 1 ) ) {
    				$_s = $wpdb->esc_like( stripslashes( wp_specialchars_decode( trim( $s ), ENT_QUOTES ) ) );
    
    				$_where[] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", '%' . $_s . '%' );
    				$_where[] = $wpdb->prepare( "{$table}._search_title LIKE %s", '%' . $_s . '%' );
    
    				if ( ! empty( $search_fields ) ) {
    					foreach ( $search_fields as $search_field ) {
    						$_where[] = $wpdb->prepare( "{$table}.{$search_field} LIKE %s", '%' . $_s . '%' );
    					}
    				}
    			}
    
    			foreach ( $keywords as $keyword ) {
    				$keyword = stripslashes( wp_specialchars_decode( trim( $keyword ), ENT_QUOTES ) );
    
    				if ( $keyword != '' ) {
    					$parts = array();
    					$parts[] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", '%' . $wpdb->esc_like( $keyword ) . '%' );
    					$parts[] = $wpdb->prepare( "{$table}._search_title LIKE %s", '%' . $wpdb->esc_like( $keyword ) . '%' );
    
    					// custom fields
    					if ( ! empty( $search_fields ) ) {
    						foreach ( $search_fields as $search_field ) {
    							$parts[] = $wpdb->prepare( "{$table}.{$search_field} LIKE %s", '%' . $wpdb->esc_like( $keyword ) . '%' );
    						}
    					}
    
    					$_where[] = implode( " OR ", $parts );
    				}
    			}
    
    			if ( ! empty( $_where ) ) {
    				$where = " OR " . implode( " OR ", $_where );
    			}
    		}
    	}
    
    	return $where;
    }
    add_filter( 'geodir_search_better_search_terms_parts', 'gd_snippet_advanced_wildcard_search_title_where', 10, 2 );
    
    function gd_snippet_advanced_wildcard_search_content_where( $where ) {
    	global $wpdb, $s, $geodir_post_type, $gd_exact_search;
    
    	if ( $s != '' && ! $gd_exact_search ) {
    		$table = geodir_db_cpt_table( $geodir_post_type );
    		$keywords = array_unique( explode( " ", $s ) );
    
    		if ( is_array( $keywords ) && ( $klimit = (int) geodir_get_option( 'search_word_limit' ) ) ) {
    			foreach ( $keywords as $kkey => $kword ) {
    				if ( geodir_utf8_strlen( $kword ) <= $klimit ) {
    					unset( $keywords[ $kkey ] );
    				}
    			}
    		}
    
    		if ( empty( $keywords ) ) {
    			$keywords = array( $s );
    		}
    
    		$_where = array();
    		if ( ! empty( $keywords ) ) {
    			foreach ( $keywords as $keyword ) {
    				$keyword = stripslashes( wp_specialchars_decode( trim( $keyword ), ENT_QUOTES ) );
    
    				if ( $keyword != '' ) {
    					$_where[] = $wpdb->prepare( "$wpdb->posts.post_content LIKE %s", '%' . $wpdb->esc_like( $keyword ) . '%' );
    				}
    			}
    
    			if ( $s == implode( " ", $keywords ) && count( $keywords ) == 1 ) {
    				$where = '';
    			} else {
    				$where = $wpdb->prepare( "OR $wpdb->posts.post_content LIKE %s", '%' . $wpdb->esc_like( stripslashes( wp_specialchars_decode( trim( $s ), ENT_QUOTES ) ) ) . '%' );
    			}
    
    			$_where = count( $_where ) > 1 ? '( ' . implode( " AND ", $_where ) . ' )' : implode( " AND ", $_where );
    			$where .= ! empty( $_where ) ? " OR " . $_where . " " : '';
    		}
    	}
    
    	return $where;
    }
    add_filter( 'geodir_search_content_where', 'gd_snippet_advanced_wildcard_search_content_where', 99, 1 );
    
    function gd_snippet_advanced_wildcard_search_posts_where( $where, $query, $geodir_post_type ) {
    	global $wpdb, $s;
    
    	if ( $s != '' && $where != '' ) {
    		$where = str_replace( $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", array( $s ) ), $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", array( $wpdb->esc_like( $s ) ) ), $where );
    	}
    
    	return $where;
    }
    add_filter( 'geodir_main_query_posts_where', 'gd_snippet_advanced_wildcard_search_posts_where', 10, 3 );

    Thanks,

    Stiofan

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change Wildcard Search Behavior’ is closed to new replies.