Thread Starter
Ahmet
(@ahmetp83)
Okay, this works:
URL/?s=keyword&post_type=product&product_cat=ID
This also works
URL/?s=keyword&post_type=product&product_tag=ID
But this does not work:
URL/?s=keyword&post_type=product&product_cat=ID1+ID2 for example product_cat=5+10
Changing the operator OR to AND did not help.
But when the plugin deactivates, this simple URL just works:
URL/shop/tag/ID1+ID2/?s=keyword
-
This reply was modified 2 years, 5 months ago by
Ahmet.
Relevanssi has full support for taxonomy filtering, but it doesn’t read all possible URL variations.
Some of these should work. At least these work on my test site:
/?s=keyword&tag=tag1+tag2
/?s=keyword&tag=tag1,tag2
/tag/tag1/?s=keyword
/tag/tag1+tag2/?s=keyword
I did test custom taxonomies on my test site, and /taxonomy/tag1+tag2/?s=keyword
still worked as expected. I haven’t tried this with WooCommerce, and I don’t know if there’s something unusual on your site, but rest assured, there’s a lot of code in Relevanssi for taxonomy filtering.
Anyway, you can fix all problems with this using a filter function on the relevanssi_modify_wp_query
hook (see instructions here) that reads the query variables and creates a tax_query
.
The function filters the $query
object, which should have all the query variables in $query->query_vars
. Read them from there, construct a well-formed tax_query
array and store it with $query->set( 'tax_query', $tax_query );
.
Thread Starter
Ahmet
(@ahmetp83)
Hi Mikko,
Thank you very much.
I found a filter on your website and added it to functions.php
add_filter( 'relevanssi_modify_wp_query', 'rlv_include_product_cat' );
function rlv_include_product_cat( $query ) {
if ( isset( $query->query_vars['product_tag'] ) ) {
$query->query_vars['tax_query'][] = array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $query->query_vars['product_tag'],
'include_children' => false,
);
}
return $query;
}
Now this works:
URL/?s=keyword – found 10 posts
URL/shop/tag/book/?s=keyword – found 6 posts
However, this does not work
URL/shop/tag/book+car/?s=keyword – found 10 posts (without the plugin found 3 posts)
I tried ‘operator’ => ‘AND’, but did not help.
Thread Starter
Ahmet
(@ahmetp83)
Hi Mikko,
Yes, when deactivating the woocommerce, it filters as tag1+tag2. (posts)
But I need to use woocommerce for my products.
When you are using multiple values in terms
, the value should be an array. Using explode( '+', $query->query_vars['product_tag'] )
instead of just $query->query_vars['product_tag']
should help.
Thread Starter
Ahmet
(@ahmetp83)
Thank you so much! I really appreciate your help.
And also need to add 'operator' => 'AND',
Here is the full code for the product tag:
add_filter( 'relevanssi_modify_wp_query', 'rlv_include_product_cat' );
function rlv_include_product_cat( $query ) {
if ( isset( $query->query_vars['product_tag'] ) ) {
$query->query_vars['tax_query'][] = array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => explode( '+', $query->query_vars['product_tag'] ),
'include_children' => false,
'operator' => 'AND',
);
}
return $query;
}