I'm trying to make a wordpress search that has autocomplete. The thing is I don't want to autocomplete the input with a post title but with categories.
I explain my problem:
I have a taxonomy called "produits" having categories in it like "diesel, kenzo, Louboutin,..." and I have a search field.
What I want to do is that the search field shows the categories: diesel, kenzo, ...
I know how to get the post title but I don't know how to do that with taxonomies.
Here is my code:
Searchform
<form id="searched" action="<?php bloginfo('url'); ?>" method="get">
<input id="search" class="autoEmpty" name="s" type="text" placeholder="Quelle réparation cherchez-vous?" autocomplete="false"><input id="search_submit" value="Rechercher" type="submit">
</form>
script in header.php
<script>
$(document).ready(function(){
$("#search").autocomplete({ // myevent_name is the id of the textbox on which we are applying auto complete process
source:'<?php echo get_bloginfo("template_url");?>/search_results.php', // sunil_results.php is the file having all the results
minLength:1,
});
});
</script>
And finally my search-results.php
<?php
include('../../../wp-load.php');
$term=$_GET["term"];
$my_args = array(
'post_type' => 'products', // post type name
"s" => $term //term we are putting in the textbox
);
$json=array();
$custom_query = new WP_Query( $my_args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
$json[]=array( 'value'=> get_the_title() );
} // while loop ends
}
echo json_encode($json);
?>
Basically that's where I need to get the post categories in json format.
Thanks a lot guys !
s
parameter doesn't search taxonomies, just post title and content. if you want posts with the selected term you need atax_query
.tax_query
.get_term_by
? It's not really clear what the point of returning term names would be though.