Remove category from algolia search

I am new to coding. I want to exclude the “Uncategorized” category from the Algolia search. Here is the category what I want to hide.
wp-admin/term.php?taxonomy=category&tag_ID=1

I tried in this way. But no luck. Please help.

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 = 'uncategorized';

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

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

    return $should_index;
}

Hi @mayuripeiris, If you are creating your own WordPress plugin according to our guide, you will need to create that filter in your code much like the ‘algolia_index_name’ filter that’s created here. If you don’t have an ‘apply_filters’ in your indexing method, the filter you have above will never be triggered.

1 Like

Hi @cindy.cullen, thank you for your reply. I am using a custom WordPress plugin according to your guide. As you said writing the code without ‘apply_filters’ may be the reason. I would be grateful to you if you could tell me how the code should be to trigger properly. At least an example of excluding categories is better. Thank you.

Hey there!

I’ve noticed in your code that $categories_to_exclude is a string, but you’re trying to use it as an array with the in_array function. This likely evaluates to null right now, so you don’t enter your condition and return false as you would expect.

You can fix this by making it an array instead:

$categories_to_exclude = ['uncategorized'];

Also, I would make sure to make terms case-insensitive before comparing:

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

Hi @sarah.dayan, Thank you for replying. I tried as you instructed. But still ‘uncategorized’ displays.

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 = ['uncategorized'];

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

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

    return $should_index;
}

Instead of ($term->term_id) I tried with ($term->slug) too. But no luck.
Appreciate your/any other ones’ further support.

I think the problem might come from the usage of the $term object. According to the documentation, $term->term_id is an integer, so the equality check with the term “uncategorized” will always return false.

You mentioned that you tried $term->slug, which failed as well. Were you able to dump the content of the slug, to see what the value is? You can do so by calling var_dump in your code.

function custom_should_index_term($should_index, WP_Term $term) {
    var_dump($term->slug);

    // ...
}

If you find that $term->slug is helpful, maybe $term->name can help. I’d recommend trying this out next, once you’ve figured out what the slug is.

1 Like

Hi @sarah.dayan I tried with both var_dump($term->slug) and die(). But cannot debug and nothing displays. It seems that nothing goes inside the function which I created in functions.php

Ma’am, do you have any idea to change the full function and exclude categories?

Hi @mayuripeiris,
To get back to the original request, it seems you want to exclude a category (“Uncategorized”) of results from your search.

To get back to Cindy’s original suggestion, have you included apply_filters as shown here: https://www.algolia.com/doc/integration/wordpress/indexing/importing-content/?language=php#customizing-the-algolia-index-name? If you don’t have an ‘apply_filters’ in your indexing method, the filter you have above will never be triggered.

Alternatively, have you tried indexing only the desired subset of posts as documented here: https://www.algolia.com/doc/integration/wordpress/indexing/importing-content/?language=php#indexing-only-a-subset-of-posts ?

1 Like