Usually, WordPress supports one category, one tag and one taxonomy in the search, if you use the query variables to set the taxonomies. You can enter multiple taxonomies as parameters to the query, but only the first one is used.
If you want to have multiple taxonomies involved, you need to build a tax_query. The correct format is described in the Codex.
Of course, you can’t just pass the array in a query variable, so you’ll have to build it. Here’s how you do a two-taxonomy search.
Search form
First, we need to add the inputs to the search form. For taxonomies, having a dropdown select is usually the best option. You can create dropdowns with the wp_dropdown_categories() function. It works for all taxonomies, despite the name. The important thing is to change the ‘name’ parameter for each dropdown.
So, in our case, we would have two dropdowns, like this, in the search form code:
wp_dropdown_categories( array( 'name' => 'movie_director', 'taxonomy' => 'director' ) ); wp_dropdown_categories( array( 'name' => 'movie_actor', 'taxonomy' => 'actor' ) );
Introducing the query variables
WordPress cleans out unknown query variables for data hygiene reasons. That’s a good thing, but a bit of a complication for us, because our query variables are unknown to WordPress. We need to introduce them. Add this to your site:
add_filter( 'query_vars', 'introduce_qvs' );
function introduce_qvs( $qv ) {
$qv[] = 'movie_director';
$qv[] = 'movie_actor';
return $qv;
}This lets WordPress know these query variables are cool.
Formatting the tax query
Now we just need to format the tax query from the query variables. This happens in the relevanssi_modify_wp_query filter, which is executed before Relevanssi search and which is therefore very good when you need to modify the query parameters in any way.
add_filter( 'relevanssi_modify_wp_query', 'movie_tax_query' );
function movie_tax_query( $query ) {
$tax_query = array();
if ( ! empty( $query->query_vars['movie_director'] ) ) {
$tax_query[] = array(
'taxonomy' => 'director',
'field' => 'term_id',
'terms' => explode( ',', $query->query_vars['movie_director'] ), // to support comma-separated values
'operator' => 'IN', // or 'AND', if you require all values to match, or 'NOT IN'
);
}
if ( ! empty( $query->query_vars['movie_actor'] ) ) {
$tax_query[] = array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => explode( ',', $query->query_vars['movie_actor'] ),
'operator' => 'IN',
);
}
if ( ! empty( $tax_query ) ) {
$tax_query['relation'] = 'AND'; // you can also use 'OR' here
$query->set( 'tax_query', $tax_query );
}
return $query;
}This should do the trick.