How to exclude all posts types by default?

wordpress

#1

Using the Algolia WordPress plugin I want to exclude ALL posts by default, and then manually include only the ones I need.

This will avoid a situation in the future when I add extra post types to my website, and forget they are being uploaded into my search index.

There is helpful code found here: https://community.algolia.com/wordpress/indexing-flow.html. But it does not achieve what I need.

This code block lets me manually exclude posts one by one from the index. But I need to exclude them ALL by default, and then manually allow only the ones I need:

/**
 * @param bool    $should_index
 * @param WP_Post $post
 *
 * @return bool
 */
function exclude_post_types( $should_index, WP_Post $post )
{
    // Add all post types you don't want to make searchable.
    $excluded_post_types = array( 'page' );
    if ( false === $should_index ) {
        return false;
    }

    return ! in_array( $post->post_type, $excluded_post_types, true );
}

// Hook into Algolia to manipulate the post that should be indexed.
add_filter( 'algolia_should_index_searchable_post', 'exclude_post_types', 10, 2 );

I also tried using this code block, taken from the same help doc which is supposed to “manually determine the post types you want to index” but it does not exclude anything by default:

add_filter( 'algolia_searchable_post_types', function( $post_types ) {
    $post_types[] = 'custom_post_type';

    return $post_types;
} );

#2

Hello,

Could you tell me how you plan to whitelist the posts you want to index?

The code snippets you listed allow to include/exclude post types from the searchables posts index.
This will not remove anything from other indices you might use.


#3

Hi @rayrutjes,

I want to whitelist the posts for indexing using a similar functions.php code.

The problem is that your current plugin automatically includes all post types in the index, and then I have to go in and exclude post types one by one using a custom code snippet.

I need the opposite… automatically exclude all post types from the index (do not send any of them for upload into the index), and then I can go in and manually include post types one by one as I need using a custom code snippet in my functions.php file.

Because if I add a custom post type to my site down the road I do not want it to be automatically uploaded and included in the index. Automatic inclusion in the index is bad, manual inclusion in the index is good :slight_smile:


#4

Ok I found a solution, here some code to put in your functions.php file incase anyone else wants the same.

Don’t forget to change ‘YOUR_POST_GOES HERE_1’ and ‘YOUR_POST_GOES HERE_2’ with your post type slugs. You can also add more if you want, just make a comma separated list wrapped in quotes.

/* Exclude all posts types from the Algolia index unless specified here */
add_filter( 'algolia_should_index_searchable_post', 'ag_custom_exclude_post_types', 10, 2 );
function ag_custom_exclude_post_types( $should_index, WP_Post $post )
{
    // Get all the post types for removal
    $all_post_types = get_post_types();
    // Don't remove these specific post types. E.g. post types listed in this array will the only ones included in the index
    $remove_some_post_types = array_diff($all_post_types, array('YOUR_POST_GOES HERE_1', 'YOUR_POST_GOES HERE_2'));
    if ( false === $should_index ) {
        return false;
    }

    return ! in_array( $post->post_type, $remove_some_post_types, true );
}

#5

Thank you @thecashbag

This is precisely what I’m trying to do. I’ve modified it so that I can presumably limit search index results to only show items from the products custom post type (in our case). For some reason, I’m getting nothing back. Is there another step or something I need to do in order to get the products to show? They exist in their own index, so I know they’re sync’ing. Just not sure why I wouldn’t get anything back in the search results.

Your snippet did remove everything else. I’m just not getting back what I think I should be getting back (product posts from the products custom post type).

Maybe I’m just missing something simple, though :slight_smile:


#6

Thankfully, I found the issue this morning with fresh eyes and a friendly nudge in the right direction. I needed to make sure I had exclude_from_search set to true in my post type declaration. Everything else was right.

My goal was to get the product custom post type to index and work, while utilizing the product_description for search relevancy, and ignoring all other post types in the search results except product in the search results returned to a user on the frontend.

I also ended up needing to use the algolia_posts_index_settings hook. For some reason the other example wasn’t working for me.

Here’s an example of how I did it. I hope this helps someone else.

add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 );

/**
 * @param array   $attributes
 * @param WP_Post $post
 *
 * @return array
 */
function my_post_attributes( array $attributes, WP_Post $post ) {

    if ( 'product' !== $post->post_type ) {
        // We only want to add an attribute for the 'product' post type.
        // Here the post isn't a 'product', so we return the attributes unaltered.
        return $attributes;
    }

    // Get the field value with the 'get_field' method and assign it to the attributes array.
    // @see https://www.advancedcustomfields.com/resources/get_field/
    $attributes['product_description'] = get_field('product_description', $post->ID);

    // Always return the value we are filtering.
    return $attributes;
}

// add_filter( 'algolia_posts_product_index_settings', 'my_posts_index_settings' );
add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );
// We could also have used a more general hook 'algolia_posts_index_settings',
// but in that case we would have needed to add a check on the post type
// to ensure we only adjust settings for the 'product' post_type.

/**
 * @param array $settings
 *
 * @return array
 */
function my_posts_index_settings( array $settings ) {

    // Make Algolia search into the 'product_description' field when searching for results.
    // Using ordered instead of unordered would make words matching in the beginning of the attribute
    // make the record score higher.
    // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestoindex
    $settings['attributesToIndex'][] = 'unordered(product_description)';

    // Make Algolia return a pre-computed snippet of 50 chars as part of the result set.
    // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
    $settings['attributesToSnippet'][] = 'product_description:50';

    // Always return the value we are filtering.
    return $settings;
}

/* Exclude all posts types from the Algolia index unless specified here */
add_filter( 'algolia_should_index_searchable_post', 'my_custom_exclude_post_types', 10, 2 );
function my_custom_exclude_post_types( $should_index, WP_Post $post )
{
    // Get all the post types for removal
    $all_post_types = get_post_types();

    // Don't remove these specific post types. E.g. post types listed in this array will the only ones included in the index
    $remove_some_post_types = array_diff($all_post_types, array('product'));

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

    return ! in_array( $post->post_type, $remove_some_post_types, true );
}