Advertisement
Guest User

Untitled

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