Filter search results using tax_query for custom results

With this little snippet, you can modify the search results based on a custom tax_query. It will enable you to really customize your search results to a specific taxonomy and even specific terms. In this sample, we are going to display search results that are in the some_taxonomy_name taxonomy with the terms 12. If you want more information about the available parameters, you can visit the WordPress Codex. As you can see, you could create some really complex results with some pretty interesting rules.

function search_filter_tax_query($query) {
    if ( $query->is_search ){
      $taxquery = array(
            'taxonomy' => 'some_taxonomy_name',
            'terms' => array( 12 ),
            'operator'=> 'IN'
      $query->set( 'tax_query', $taxquery );
    return $query;
add_action( 'pre_get_posts', 'search_filter_tax_query' );

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!