0

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 !

6
  • are you trying to return posts with the searched term, or the terms themselves? the s parameter doesn't search taxonomies, just post title and content. if you want posts with the selected term you need a tax_query. – Milo May 20 '15 at 21:06
  • Nope I need to return each taxonomies in the search field not the title. All categories that exist. Like that when we hit the search button it will return every posts in that category ! Ex: every posts that are in louboutin ! – jhon May 20 '15 at 21:19
  • taxonomies contain terms, terms are associated to posts. if you want posts with the searched term, you want a tax_query. – Milo May 20 '15 at 21:21
  • yeah but it will return (in the search field) the post title and not the categories available. I dont know if you see what I want... It's hard to explain ! – jhon May 20 '15 at 21:31
  • then you don't want posts or any type of post query, you just want terms, maybe via get_term_by? It's not really clear what the point of returning term names would be though. – Milo May 20 '15 at 21:39
1

Ok I got it!

That's the code for my search-results.php

<?php 
include('../../../wp-load.php');

$term=$_GET["term"]; 

$json=array();

$terms = get_terms( 'produits' );
 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     foreach ( $terms as $term ) {
       $json[]=array( 'value'=> $term->name );
     }
 }
    echo json_encode($json);
?>

Thanks milo for your help ! I needed to use get_terms !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.