Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * "fuzzy search" to help with searching for profiles
- * @param string $search query based on search term
- * @param string $wp_query query based on entire page (including $search)
- * @return string sql query for page
- */
- function wpse66815_search_query_string( $search, &$wp_query )
- {
- if (!is_admin() && is_search()) {
- //print_r($search);
- global $wp_query,$wpdb;
- // get search term
- $search_term = array_shift($wp_query->query_vars['search_terms']);
- // specify string we'll use to replace
- $replace_var = 'TL';
- // find matches for that string
- preg_match_all("/{$replace_var}(?:[^0-9]*)(\d+)/i", $search_term, $out);
- // if there's no matches, return the normal search
- if ( empty($out[0]) )
- return $search;
- //var_dump($out);
- // find/generate the search term with the replacement
- $modified_search_term = preg_replace(
- "/{$replace_var}(?:[^0-9]*)(\d+)/i"
- ,"{$replace_var}-$1"
- ,$search_term
- );
- // combine both the regular and modified search term
- $new_search[] = $search_term;
- $new_search[] = $modified_search_term;
- print_r($new_search[0]);
- // generate the new search query
- $new_string_parts[] = $wpdb->prepare(
- "
- AND ((({$wpdb->posts}.post_title LIKE '%%%s%%')
- OR ({$wpdb->posts}.post_content LIKE '%%%s%%'))
- "
- ,like_escape( $new_search[0] )
- ,like_escape( $new_search[0] )
- );
- $new_string_parts[] = $wpdb->prepare(
- "
- OR (({$wpdb->posts}.post_title LIKE '%%%s%%')
- OR ({$wpdb->posts}.post_content LIKE '%%%s%%')))
- "
- ,like_escape( $new_search[1] )
- ,like_escape( $new_search[1] )
- );
- /*foreach ( $new_search as $keyword)
- {
- if ($keyword === reset($new_search)) {
- $new_string_parts[] = $wpdb->prepare(
- "
- AND ((({$wpdb->posts}.post_title LIKE '%%%s%%')
- OR ({$wpdb->posts}.post_content LIKE '%%%s%%'))
- "
- ,like_escape( $keyword )
- ,like_escape( $keyword )
- );
- }
- elseif ($keyword === end($new_search)) {
- $new_string_parts[] = $wpdb->prepare(
- "
- OR (({$wpdb->posts}.post_title LIKE '%%%s%%')
- OR ({$wpdb->posts}.post_content LIKE '%%%s%%')))
- "
- ,like_escape( $keyword )
- ,like_escape( $keyword )
- );
- }
- else {
- $new_string_parts[] = $wpdb->prepare(
- "
- OR (({$wpdb->posts}.post_title LIKE '%%%s%%')
- OR ({$wpdb->posts}.post_content LIKE '%%%s%%'))
- "
- ,like_escape( $keyword )
- ,like_escape( $keyword )
- );
- }
- }*/
- // set $search equal to results
- $search = implode( " ", $new_string_parts );
- //print_r($search);
- }
- return $search;
- }
- add_filter('posts_search', 'wpse66815_search_query_string',500,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement