So, yes, you are making use of algolia_excluded_post_types
filter, but one thing I think we need to do with better clarifying is that filter is specifically for the autocomplete page and those settings.
If you check out https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Filter-Hooks there’s a section for the Search page settings/Instantsearch, for which I’ve pasted the intended filter example below.
<?php
function my_exclude_searchable_custom_post_type( array $post_types ) {
if ( array_key_exists( 'custom_post_type', $post_types ) ) {
unset( $post_types['custom_post_type'] );
}
return $post_types;
}
add_filter( 'algolia_searchable_post_types', 'my_exclude_searchable_custom_post_type' );
As per one of the notes there:
With this hook, we’ve already fetched all the post types that have exclude_from_search
set to false, so we need to remove instead of add.
You’ll want to update the custom_post_type
part to match which you want to exclude, and repeat as necessary. Alternatively if you know exactly which post type you want, you could have the filter return something like this, for example just have it index the page
post type.
return [ 'page' => 'page' ];
Regarding the indexing/not indexing part your exclusion function may be best done as such
function algolia_exclude_extra( $should_index, $post ) {
// Check if the post type is 'page'
if ( in_array( $post->post_type, [ 'page' ] ) ) {
// Check if the post belongs to the 'Services' category
if ( ! has_category( 7, $post ) ) {
// Exclude the post from being indexed
return false;
}
}
// Allow indexing for all other cases
return $should_index;
}
add_filter( 'algolia_should_index_searchable_post', 'algolia_exclude_extra', 10, 2 );
add_filter( 'algolia_should_index_post', 'algolia_exclude_extra', 10, 2 );
From your original version, it looks like the return $should_index
line was never getting reached, because your else
clause also returned false.