Filtering results by Category AND custom Taxonomy
-
Hello,
I have created a search form that is functioning almost perfectly, but I am having an issue when trying to use and ‘Advanced Search’ to filter the results.
My users need to have the ability to choose option from a custom taxonomy (they can only choose one term from this tax) and from a list of Categories (can choose multiple terms). The problem that I am running in to is that when the user chooses options from BOTH the custom taxonomy AND the categories the results return are for those matching the custom tax OR the category. I need this to filter by AND.
The default wordpress functionality for this is AND, but when using Relevanssi the results are returned using OR. If I turn off the plugin I get the expected filtering using AND.
I have read some other posts and have seen that one issue might be that I need to create a tax_query, which I have tried but it does not seem to be doing anything.
this is the code I have tried to enter in my function.php but it does not seem to be doing anything.
add_filter('relevanssi_modify_wp_query', 'advanced_search'); function advanced_search($query) { if (!empty($query->query_vars['rtree'])) { if (!empty($_GET['cat'])) { $catArray = array(); $cats = explode(',',$_GET['cat']); foreach ( $cats as $cat ) { $catArray[] = $cat; } } $tax_query = array( 'relation' => 'AND', array( 'taxonomy' => 'rtree', 'field' => 'slug', 'terms' => $query->query_vars['rtree'] ), array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $catArray ) ); $query->set('tax_query', $tax_query); } return $query; }
The site can be seen here. clicking the ‘Advanced Search’ text under the search box will show the filter options.
Also I have the Relevanssi settings in the WP admin section set to us AND (although I think that just applies to the actual search terms).
- The topic ‘Filtering results by Category AND custom Taxonomy’ is closed to new replies.