Search code examples
javascriptwordpressalgoliainstantsearch

how to remove a category from woocommerce products through instantsearch js


I've tried it that way but not working, i think it must have some specific way to call the woocommerce category

    search = instantsearch({
      appId: 'myid',
      apiKey: 'mykey',
      indexName: 'wp_posts_product',
      searchParameters: {
        filters: filters,
        hitsPerPage: 9,
            facetsExcludes: {
                categories: ['Destaques', 'destaques']
            }
      }
    });

Solution

  • In order to filter out a specific category, you need to leverage the filters parameter of the Algolia API.

    https://www.algolia.com/doc/api-reference/api-parameters/filters/?language=javascript#combination-of-filters

    If I adjust your code, it would give you something like:

    var search = instantsearch({
      appId: "myid",
      apiKey: "mykey",
      indexName: "wp_posts_product",
      searchParameters: {
        filters: "NOT categories:Destaques",
        hitsPerPage: 9,
      }
    });
    

    Note that Algolia computes facet values in a case insensitive fashion. As a result you do not need to also specify: NOT categories:Destaques.

    If your category contains spaces, ensure you wrap it in double quotes like so: NOT categories:"Destaques".

    Finally you want to exclude multiple categories, you can combine conditions with the AND statement: NOT categories:Destaques AND NOT categories:"other with spaces".