Algolia - Wordpress - exclude category from indexing Algolia - Wordpress - exclude category from indexing wordpress wordpress

Algolia - Wordpress - exclude category from indexing


First of all, I would recommend you stick with the new version of the plugin. At the time of writing this, the latest version is 0.2.5. Indeed the old version (0.0.1) will not be supported anymore.

Regarding your question, it is indeed possible to filter what posts you would like to push to Algolia and make searchable.

What I understand from your question is that you have pages assigned to categories, and you would like to avoid making pages from certain categories come up in search results. If these initial statements are wrong, please comment on this answer and I'd gladly push an update!

You can hook into the decision of indexing a post by using WordPress filters.In your case, if you are willing to exclude the pages from the searchable_posts index you could use the algolia_should_index_searchable_post filter.If you are willing to exclude the pages from the posts_page index, you could use the algolia_should_index_post filter.

Here is an example of how you could exclude all pages of a list of categories identified by their IDs.

<?php    // functions.php of your theme    // or in a custom plugin file.    // We alter the indexing decision making for both the posts index and the searchable_posts index.     add_filter('algolia_should_index_post', 'custom_should_index_post', 10, 2);    add_filter('algolia_should_index_searchable_post', 'custom_should_index_post', 10, 2);    /**     * @param bool    $should_index     * @param WP_Post $post     *     * @return bool     */    function custom_should_index_post( $should_index, WP_Post $post ) {        // Replace these IDs with yours ;)        $categories_to_exclude = array( 7, 22 );        if ( false === $should_index ) {            // If the decision has already been taken to not index the post            // stick to that decision.            return $should_index;        }        if ( $post->post_type !== 'page' ) {            // We only want to alter the decision making for pages.            // We we are dealing with another post_type, return the $should_index as is.            return  $should_index;        }        $post_category_ids = wp_get_post_categories( $post->ID );        $remaining_category_ids = array_diff( $post_category_ids, $categories_to_exclude );        if ( count( $remaining_category_ids ) === 0 ) {            // If the post is a page and belongs to an excluded category,            // we return false to inform that we do not want to index the post.            return false;        }        return $should_index;    }

More information about extending the Algolia Search plugin for WordPress can be found on documentation: Basics of extending the plugin

Update:

The code has been updated to ensure it doesn't exclude a product if it is associated to multiple categories and not all of them are excluded.