X

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. Nothing too complex but will enable you to really customize your search results to specific taxonomy and even specific terms. So in this sample we are going to display search results that are in some_taxonomy_name taxonomy with the terms 12. If you want more information about available parameters you can visit WordPress Codex. As you can see you could create some really complex results with some pretty interesting rules.


Code Snippet:


function search_filter_tax_query($query) {
if ( $query->is_search ){
$taxquery = array(
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!