• Resolved belljr56

    (@belljr56)


    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).

    https://wordpress.org/plugins/relevanssi/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Looks like there’s a bug in Relevanssi.

    Find this code in lib/search.php:

    if (!isset($relation)) $relation = "or";
    	$relation = strtolower($relation);

    Change it to:

    if (!isset($tax_query_relation)) $tax_query_relation = "or";
    	$relation = strtolower($tax_query_relation);

    Does that fix the problem?

    Thread Starter belljr56

    (@belljr56)

    Fantastic, that did it! Thank you so much for the quick response. Will this be changed with the next release of Relevanssi, or do I need to make this code edit every time there is an update?

    Awesome plugin by the way!

    Plugin Author Mikko Saari

    (@msaari)

    I’ll make sure this is fixed in the next version.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filtering results by Category AND custom Taxonomy’ is closed to new replies.