Closed craigwilcox closed 5 months ago
Hi @craigwilcox,
Certainly, you can achieve this by adding an additional filter to the Elasticsearch query using the ep_post_filters
hook https://10up.github.io/ElasticPress/ep_post_filters.html. Your code will look something like this:
/**
* Exclude category from search.
*
* @param array $filters Filters to be applied to the query
* @param array $args WP Query args
* @param WP_Query $query WP Query object
*/
add_filter(
'ep_post_filters',
function( $filters, $args, $query ) {
if ( is_admin() ) {
return $filters;
}
if ( ! $query->is_main_query() || ! $query->is_search() ) {
return $filters;
}
$filters[] = [
'bool' => [
'must_not' => [
[
'terms' => [
'terms.product_cat.term_id' => [ 22 ], // Change 22 to the category ID you want to exclude.
],
],
],
],
];
return $filters;
},
10,
3
);
Since this isn't a bug and there is no further input is required therefore, I'm closing this issue.
Describe your question
We group some products into a category that we want to exclude from search. For instance, class tickets, which aren't really products. Is there a way we could exclude that category from search results? Let's call the category "classes-events" as an example.
Thanks!
Code of Conduct