Advertisement
Guest User

Untitled

a guest
Oct 9th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. /**
  2.  * "fuzzy search" to help with searching for profiles
  3.  * @param  string $search   query based on search term
  4.  * @param  string $wp_query query based on entire page (including $search)
  5.  * @return string           sql query for page
  6.  */
  7. function wpse66815_search_query_string( $search, &$wp_query )
  8. {
  9.     if (!is_admin() && is_search()) {
  10.  
  11.         //print_r($search);
  12.  
  13.         global $wp_query,$wpdb;
  14.  
  15.         // get search term
  16.         $search_term = array_shift($wp_query->query_vars['search_terms']);
  17.  
  18.         // specify string we'll use to replace
  19.         $replace_var = 'TL';
  20.  
  21.         // find matches for that string
  22.         preg_match_all("/{$replace_var}(?:[^0-9]*)(\d+)/i", $search_term, $out);
  23.  
  24.         // if there's no matches, return the normal search
  25.         if ( empty($out[0]) )
  26.             return $search;
  27.  
  28.         //var_dump($out);
  29.  
  30.         // find/generate the search term with the replacement
  31.         $modified_search_term = preg_replace(
  32.              "/{$replace_var}(?:[^0-9]*)(\d+)/i"
  33.             ,"{$replace_var}-$1"
  34.             ,$search_term
  35.         );
  36.  
  37.         // combine both the regular and modified search term
  38.         $new_search[] = $search_term;
  39.         $new_search[] = $modified_search_term;
  40.  
  41.         print_r($new_search[0]);
  42.  
  43.         // generate the new search query
  44.         $new_string_parts[] = $wpdb->prepare(
  45.              "
  46.                 AND ((({$wpdb->posts}.post_title LIKE '%%%s%%')
  47.                 OR ({$wpdb->posts}.post_content LIKE '%%%s%%'))
  48.              "
  49.             ,like_escape( $new_search[0] )
  50.             ,like_escape( $new_search[0] )
  51.         );
  52.  
  53.         $new_string_parts[] = $wpdb->prepare(
  54.              "
  55.                 OR (({$wpdb->posts}.post_title LIKE '%%%s%%')
  56.                 OR ({$wpdb->posts}.post_content LIKE '%%%s%%')))
  57.              "
  58.             ,like_escape( $new_search[1] )
  59.             ,like_escape( $new_search[1] )
  60.         );
  61.  
  62.         /*foreach ( $new_search as $keyword)
  63.         {
  64.             if ($keyword === reset($new_search)) {
  65.                 $new_string_parts[] = $wpdb->prepare(
  66.                      "
  67.                         AND ((({$wpdb->posts}.post_title LIKE '%%%s%%')
  68.                         OR ({$wpdb->posts}.post_content LIKE '%%%s%%'))
  69.                      "
  70.                     ,like_escape( $keyword )
  71.                     ,like_escape( $keyword )
  72.                 );
  73.             }
  74.             elseif ($keyword === end($new_search)) {
  75.                 $new_string_parts[] = $wpdb->prepare(
  76.                      "
  77.                         OR (({$wpdb->posts}.post_title LIKE '%%%s%%')
  78.                         OR ({$wpdb->posts}.post_content LIKE '%%%s%%')))
  79.                      "
  80.                     ,like_escape( $keyword )
  81.                     ,like_escape( $keyword )
  82.                 );
  83.             }
  84.             else {
  85.                 $new_string_parts[] = $wpdb->prepare(
  86.                      "
  87.                         OR (({$wpdb->posts}.post_title LIKE '%%%s%%')
  88.                         OR ({$wpdb->posts}.post_content LIKE '%%%s%%'))
  89.                      "
  90.                     ,like_escape( $keyword )
  91.                     ,like_escape( $keyword )
  92.                 );
  93.             }
  94.         }*/
  95.  
  96.         // set $search equal to results
  97.         $search = implode( " ", $new_string_parts );
  98.  
  99.         //print_r($search);
  100.     }
  101.  
  102.     return $search;
  103. }
  104. add_filter('posts_search', 'wpse66815_search_query_string',500,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement