0

On a specific page, I would like the search to be performed only against the terms from two custom taxonomies. Not the content or title. That is, the user is directly searching taxonomy terms.

What I'm trying to do is completely rewrite the query for searches, but it doesn't seem to work.

function search_filter_get_posts($query) {
    if ( !$query->is_search )
        return $query;

    $terms = explode(",", $query->query['s']);

    $taxquery = array(
        'post_type' => 'eproduct',
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'product-type',
                'field'    => 'slug',
                'terms'    => $terms,
            ),
            array(
                'taxonomy' => 'product-features',
                'field'    => 'slug',
                'terms'    => $terms,
            ),
        ),
    );
;
 $query = new WP_Query( $taxquery );

}
add_action( 'pre_get_posts', 'search_filter_get_posts' );
  • 1
    There isn't any mistake in your code. I'd check what is in $query->query['s']. – Emetrop Aug 24 '15 at 17:43
1

You did not return the new $query; that's why it's not working for you.

$query = new WP_Query( $taxquery );    
return $query;
0

I think, the problem is that you just put filtered query in $query variable but did not set it in the main query i.e. $query->set()

See if this works in your code -

$query->set( 'tax_query', $taxquery );

instead of

$query = new WP_Query( $taxquery );

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.