http://iamcal.com/publish/articles/php/search/ */ function search_split_terms($terms){ $terms = preg_replace("/\"(.*?)\"/e", "search_transform_term('\$1')", $terms); $terms = preg_split("/\s+|,|-/", $terms); $out = array(); foreach($terms as $term){ $term = preg_replace("/\{WHITESPACE-([0-9]+)\}/e", "chr(\$1)", $term); $term = preg_replace("/\{COMMA\}/", ",", $term); $out[] = $term; } return $out; } function search_transform_term($term){ $term = preg_replace("/(\s)/e", "'{WHITESPACE-'.ord('\$1').'}'", $term); $term = preg_replace("/,/", "{COMMA}", $term); return $term; } function search_escape_rlike($string){ return preg_replace("/([.\[\]*^\$])/", '\\\$1', $string); } function search_db_escape_terms($terms){ $out = array(); foreach($terms as $term){ $out[] = '[[:<:]]'.AddSlashes(search_escape_rlike($term)).'[[:>:]]'; } return $out; } function tm_searchrelated(){ global $wpdb,$wp_rewrite; // if mod-rewrite is active if(isset($_GET['name'])){ $terms = $_GET['name']; }; //if mockup mod-rewrite is active, index.php/year/month/date/post_title style if(!isset($terms)){ $rewrite = $wp_rewrite->wp_rewrite_rules(); $pathinfo = $_SERVER['PATH_INFO']; $req_uri = $_SERVER['REQUEST_URI']; $home_path = parse_url(get_settings('home')); $home_path = $home_path['path']; $req_uri = str_replace($pathinfo, '', $req_uri); $req_uri = str_replace($home_path, '', $req_uri); $req_uri = trim($req_uri, '/'); $pathinfo = trim($pathinfo, '/'); if (! empty($pathinfo)) { $request = $pathinfo; } else { $request = $req_uri; } $request_match = $request; foreach ($rewrite as $match => $query) { if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) { $request_match = $req_uri . '/' . $request; } if (preg_match("!^$match!", $request_match, $matches)) { $query = preg_replace("!^.+\?!", '', $query); eval("\$query = \"$query\";"); } } $query = explode("name=",$query); $query = explode("&",$query[1]); $terms = $query[0]; $test_t = get_the_title(); }; if(strlen($test_t)>0){ $terms = $test_t; }; $t = $terms; $key = "post_title"; $what = "post_title, ID, post_name"; $table = $wpdb->posts; $like = "post_title"; $like_404 = "post_content"; $terms = search_split_terms($terms); $terms_db = search_db_escape_terms($terms); $parts = array(); foreach($terms_db as $term_db){ $parts[] = "$like RLIKE '$term_db'"; } $parts = implode(' OR ', $parts); // Only one search term, so let's use existing topics to find a match inside the term if(count($terms)==1){ $sql = "SELECT $what FROM $table"; $query = $wpdb->get_results($sql, ARRAY_A); foreach($query as $q){ $key_t = explode(" ",$q[$key]); foreach($key_t as $p){ if(strlen($p)>0){ if(stristr($t,$p)){ if($q[$key] != $t){ $result_top[] = ''.$q[$key].''; }; }; }; }; }; }; // More than one term, easier to search for. if(count($terms)>1){ $sql = "SELECT $what FROM $table WHERE $parts"; $query = $wpdb->get_results($sql, ARRAY_A); foreach($query as $q){ if($q[$key] != $t){ $result_top[] = ''.$q[ $key].''; }; }; }; if(!isset($result_top)){ $result_top[] = "No related topics found."; }; sort($result_top); foreach($result_top as $r){ $return_top .= $r."
"; }; echo"Related Topics:
"; echo $return_top; }; ?>