Algolia - Wordpress - exclude category from indexing
Asked Answered
D

1

6

How can I exclude certain WordPress Page categories from being indexed in Algolia?

Dunlin answered 28/7, 2016 at 6:54 Comment(2)
Could you tell us the version of the plugin you are using?Repartition
Currently - Version 0.1 I tried also Version 0.2.4 - but i couldn't exclude particular category - only Categories as generalDunlin
R
5

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.

Repartition answered 30/7, 2016 at 13:17 Comment(7)
thank you! I downloaded version 0.2.8 and i tried the code above in my theme's functions.php and hit reindex in wordpress admin panel, no records were indexed to Algolia.Dunlin
Any help with this issue? - when activating the code above (in a plugin file) with my IDs and re-indexing, no records are indexed to Algolia at all.Dunlin
No errors were logged in the Log menu of Algolia plugin. Here is the code i use: codeshare.io/CFu6VDunlin
Would you mind trying out the latest version of the plugin? wordpress.org/plugins/… Be sure to uninstall previous version first. You can install the plugin directly from the WordPress plugins section from now on.Repartition
The code solution was missing the actual filter callbacks. It should now work better.Repartition
Hmm, it is still being indexed :/Dunlin
We had another user successfully using the corrected snippet. Could you share your whole version so that we can check it? Also did you correctly replace the category ids with your own?Repartition

© 2022 - 2024 — McMap. All rights reserved.