Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Algolia indexing certain post types

Tags:

algolia

I have the algolia search working on my site but in the backend its also indexing a post type which should never be indexed (as it contains private infromation).

Is there a setting somewhere where I can say "NEVER index {post_type}"?

like image 681
Craig Edmonds Avatar asked Feb 05 '23 06:02

Craig Edmonds


1 Answers

If you're speaking about an Algolia for WordPress plugin, you can definitely define a function/hook to decide whether a post type should be indexed or not.

For instance you could write:

<?php
/**
 * @param bool    $should_index
 * @param WP_Post $post
 *
 * @return bool
 */
function exclude_post_types( $should_index, WP_Post $post )
{
    if ( false === $should_index ) {
        return false;
    }

    // Add all post types you don't want to make searchable.
    $excluded_post_types = array( 'myprivatetype' );    
    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 );

You can read more on: https://community.algolia.com/wordpress/indexing-flow.html#indexing-decision

like image 183
redox Avatar answered Feb 27 '23 15:02

redox