2

So I have custom taxonomy called "cars". I would want that when user does search, it would also search through taxonomy. Meaning that in "cars" taxonomy if I have "Audi" term and user search with keyword "audi" it would then return posts that are tagged with term "audi".

I tried this:

function mytheme_pre_get_posts( $query ) {
$query->set( 'tax_query', array(
        array(
            'taxonomy' => 'cars',
            'field'    => 'name',
            'terms'    =>  $_GET['s'],
        )
    ));
add_action( 'pre_get_posts', 'mytheme_pre_get_posts', 1 );

But that breaks the search completely. Can this be done even or did I miss something?

1
  • Also note that you're modyfing all WP_Query calls on your site with this code snippet, so I think you want to restrict it to the main search query in the front-end?
    – birgire
    Jun 3, 2016 at 11:45

1 Answer 1

2

You could try unsetting the original search query, otherwise you may be doubling up and the combination results in nothing? eg, try adding to your function:

$query->set('s','');

UPDATE

I am not sure on this one, but you could try the AND operator -I think this adds to the existing query instead of replacing it...

function mytheme_pre_get_posts( $query ) {
    if ( !is_admin() && $query->is_search() && $query->is_main_query() ) {
        $term = get_term_by('name', get_query_var('s'), 'cars');
        if ($term) {
            $query->set( 'tax_query', array(
                 array(
                    'taxonomy' => 'cars',
                    'field'    => 'slug',
                    'terms'    =>  $term->slug,
                    'operator' => 'AND'
                )
            ));
         }
    }
}
add_action( 'pre_get_posts', 'mytheme_pre_get_posts', 1 );

Note: changed use of field name as mentioned by @PieterGoosen to slug, and pre-fetched term slug to pass to tax query, and extra conditions as mentioned by @birgire.

9
  • But does normal keyword search still work? Maybe I missed from my original post, that the normal keyword (for example title) should still work. Searching from taxonomy is just addition to the default search. EDIT. I tried and doesn't help. This will loose the searched keyword from field too.
    – user995317
    Jun 3, 2016 at 9:24
  • yeah I thought you wanted to replace the existing search... another suggestion in the update.
    – majick
    Jun 3, 2016 at 11:13
  • 1
    You do not want to use the name value field in your tax_query. There is a known "bug" in the WP_Tax_Query class that completely stuffs up term names which causes an epic failure and leads to the JOIN clause never being added to the SQL query Jun 3, 2016 at 11:20
  • good to know, have updated the code to use slug... hopefully slugs, being molluscs, are bug free. ;-)
    – majick
    Jun 3, 2016 at 11:29
  • 1
    I think we need to be more restrict because is_search() can be true for all secondary queries as well, since this is only a check for the global $wp_query main query. I would try $query->is_main_query() and also &query->is_search() and then perhaps ! is_admin() for the front-end only?
    – birgire
    Jun 3, 2016 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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