Hello,
i need to filter the results of product text search to be limited to the products that are in a specific category!
I already did something really similar, to display everywhere in my site only products that have a specific category.
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
//if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() || is_filtered() || is_search() ) ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'web' ), // Display only products in the "web" category on the shop page
'operator' => 'IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Now i need something similar to work both on the search result archive page, (i added is_search() but turns out it only works for normal wordpress search result pages), and for the ajax search too…
Even if you can only point me in the right direction i would appreciate.
Thank You.