WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
146 stars 55 forks source link

Filtering Search Results #353

Closed michael-supreme closed 1 year ago

michael-supreme commented 1 year ago

What would be the best method to add a hidden filter to all search results so that in all cases, only certain posts are returned when using the native WordPress + Algolia search template?

I have a taxonomies.translation_priority in my index parameters, and I'd like to filter it so that in all cases, only results that equal "Optional" are returned. (I realize I can modify the indexed database, but it's rather large and I'd like to avoid touching it, if possible). image

I tried placing the following code in my functions.php, with no luck. (Checking the Search API Logs in Algolia shows no filtering of any kind)

function my_algolia_query_filters( $params ) {
    $params['filters'] = [ "taxonomies.translation_priority = Optional" ];

    return $params;
}
add_filter( 'algolia_search_params', 'my_algolia_query_filters', 10, 1 );

Also, to clarify, this is on the search page, not autocomplete (I saw the other post where a user was having a similar issue with autocomplete)

tw2113 commented 1 year ago

Hi @michael-supreme

Looking at the code, that specific filter is meant more for when using with backend/native search more than Instantsearch. I could be wrong on the potential applicable feasibility of the filter aspect like that.

I do wonder if you maybe need to check out https://www.algolia.com/doc/guides/managing-results/refine-results/filtering/how-to/filter-by-attributes/ and try things out in the format demo'd there.

Otherwise, it may be a case of needing to update/customize the InstantSearch template, perhaps in a similar way as shown at the end of https://github.com/WebDevStudios/wp-search-with-algolia/wiki/WPML

michael-supreme commented 1 year ago

Hey @tw2113, thank you for the link - This helped me get it working with a few minor modifications!