0) // make sure it's not something_name { $x = strpos($data, "&name="); if (!($x===FALSE)) ++$x; } if (!($x===FALSE)) { $match=substr($data, $x+5); $length=strcspn($match, "&"); $match=substr($match, 0, $length); $out = substr($data, 0, $x) . "name=$phord_unique_flexurl_marker" . substr($data, $x+5+$length); $phord_old_query = $match; } return $out; } // This filter gets called to parse the "where" clause for MYSQL for *all* lookups. // Be very, very careful in here. // We are essentially changing the query from "name = 'some_cutoff_name'" to "name LIKE 'some_cutoff_name%'" // We use a little private marker up in phord_query_filter to make this easier. function phord_where_filter($data) { global $phord_old_query, $phord_unique_flexurl_marker; global $phord_flexurl_pending_multi; $phord_flexurl_pending_multi=FALSE; if (''==phord_old_query) return $data; $out=$data; $x = strpos($data, "= '$phord_unique_flexurl_marker'"); $length = strlen("= '$phord_unique_flexurl_marker'"); if (!($x===FALSE)) { $out = substr($data, 0, $x) . " like '$phord_old_query%' " . substr($data, $x+$length); $phord_flexurl_pending_multi = TRUE; } return $out; } // This filter gets called after the list of posts is retrieved. // We need to adjust some things to handle multiple posts in case // multiple posts were returned even though the query was supposed // to be for a single post originally. function phord_posts_filter($posts) { global $phord_flexurl_pending_multi; global $wp_query; global $phord_old_query; if ($phord_flexurl_pending_multi) { if (count($posts) > 1) { $wp_query->is_single = FALSE; } else { // Here we did a search on a partial or complete name that returned exactly one entry. // Check if the search name was complete. If not, redirect to the correct location. $id = rtrim(get_permalink($posts[0]->ID), "/")."/"; if (stristr($id, $phord_old_query."/") === FALSE) { // echo ""; HEADER ( "HTTP/1.1 301 Moved Permanently" ); HEADER ( "Location: " . get_permalink($posts[0]->ID) ); exit(0); } } } $phord_flexurl_pending_multi=FALSE; return $posts; } // Attach all our filters add_filter('query_string', 'phord_query_filter'); add_filter('posts_where', 'phord_where_filter'); add_filter('the_posts', 'phord_posts_filter'); // make sure there's no blank spaces or lines after this line: ?>