Woocommerce - Hide some categories in autocomplete dropdown

Hello,

We are working on a woocommerce website. Is there away to hide some categories in the autocomplete dropdown, but actually index their products ? These categories are naturally referenced so we don’t want to hide them, we just don’t want them in the dropdown.

Thank you.

1 Like

Hello,

In order to not index some categories, you can use the following filter: algolia_should_index_term.

It is defined here: https://github.com/algolia/algoliasearch-wordpress/blob/master/includes/indices/class-algolia-terms-index.php#L42

And will determine wether or not to index a given category.

The products are indexed independently from the terms index so it should achieve what you want.

It is up to you to define how to match the categories to include or exclude.

Cheers,

Hello,

I tried with the following :

add_filter('algolia_should_index_term', 'custom_should_index_term', 10, 2);
function custom_should_index_term( $should_index, WP_Term $term ) {
    
    $categories_to_exclude = 'category-name';

    if ( false === $should_index ) {
        return $should_index;
    }

    if ( in_array($term->term_id, $categories_to_exclude ) ) {
        return false;
    }

    return $should_index;
}

The category is still showing in the autocomplete dropdown after reindexing.

What am I doing wrong ?

Thank you.

This seems to be the way to go.

Can you add some debug code to see if the categories are passed through this filter and print the term_id and what’s in $categories_to_exclude.

Please let me know what you find.