I've built an Autocompleting search box for WordPress categories (it's actually a Custom Taxonomy), which works great (i.e it shows the categories when typed). However, when 1 key is typed, it will show all the results, regardless of the fact whether that string matches any of the categories.
Here is my PHP code (autocomplete.php, in the root of my theme directory):
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' ); // I know the traditional way to do it is loading admin_ajax but I read this was exactly the same and didn't increase the overhead to the server
if (isset($_GET['term'])) { // I suspect the problem is something to with this
$tradeList = get_terms('trade');
$tradeNames = Array();
foreach ($tradeList as $trade) {
$tradeName = $trade->name;
array_push($tradeNames, $tradeName);
}
echo json_encode($tradeNames);
}
?>
and my Javascript code:
$(document).ready(function() {
$(".main-search-field").autocomplete({
source: "/wp-content/themes/local_directory/autocomplete.php",
minLength: 1
});
});
and finally HTML:
<input type="text" class="main-search-field" name="search" id="search">
I realise this might be badly coded, if it is feel free to re-do it. But what I really need to know is how to get the autocomplete suggestions to be relevant to the string that is typed in the search box.
Thanks for any help :)