Filter results from autocomplete in WordPress

Hey!

How can I filter out results from the autocomplete dropdown based on an attribute? I’ve thought about adding filters to the source: algoliaAutocomplete.sources.hits second parameter object (found in autocomplete.php template):

source: algoliaAutocomplete.sources.hits(client.initIndex(config['index_name']), {
	hitsPerPage: config['max_suggestions'],
	attributesToSnippet: [
		'content:10'
	],
	highlightPreTag: '__ais-highlight__',
	highlightPostTag: '__/ais-highlight__',
	filters: 'NOT taxonomies.product_visibility:outofstock',
}),

Basically I want to filter out products that are sold out. However, this is not working. The results still show out of stock products. What is the correct way to use filters here? Do I need to use a WordPress filter for this?

Hi @arjen1,

The first thing to confirm is that your attribute taxonomies.product_visibility is set as an attributesForFaceting, otherwise a filter will not work on the attribute. You can also set this in the Dashboard > “Configuration” > “Filtering and Faceting”:

Thereafter your syntax filters: 'NOT taxonomies.product_visibility:outofstock should work.

I made a test index with a simple record structure:

{
  "name": "in stock",
  "taxonomies": {
    "product_visibility": "instock"
   }
}

{
  "name": "out of stock",
  "taxonomies": {
    "product_visibility": "outofstock"
   }
 }

Please see the GIF below and the codesandbox showing this behavior:

Hey @ajay.david,

Thank you! I didn’t set an attributesForFacting. It works now that I’ve set it there.