• Resolved alexryans

    (@alexryans)


    Hi,

    Is it possible to hide certain pages from being returned by the autocomplete? I would like to hide a few WooCommerce pages, such as the Account page.

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    From what I know and understand, anything that’s indexed is available for the autocomplete, so to get those specific pages removed from the suggestions, they’ll need to be removed from the index as well.

    Thread Starter alexryans

    (@alexryans)

    OK, great, thanks, that makes sense! How would I go about deleting certain pages from the posts_page index?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    This would probably put you on a good step forward

    https://stackoverflow.com/questions/38629094/algolia-wordpress-exclude-category-from-indexing

    You won’t need ALL of it, but it shows how things could be adapted to check for your specific conditions and return whether or not the item should get indexed.

    Thread Starter alexryans

    (@alexryans)

    Hi Michael,

    That’s great, thank you – I’ll take a look at that article and get back in touch with any further questions.

    Thread Starter alexryans

    (@alexryans)

    For completion purposes, here’s the final code I used to achieve this:

    
    // 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) {
        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;
        }
    
        $woocommerce_page_ids = [
            get_option('woocommerce_cart_page_id'),
            get_option('woocommerce_checkout_page_id'),
            get_option('woocommerce_myaccount_page_id')
        ];
    
        if (in_array($post->ID, $woocommerce_page_ids)) {
            // If the post is a page and has an excluded page ID,
            // we return false to inform that we do not want to index the post.
            return false;
        }
    
        return $should_index;
    }
    
    • This reply was modified 3 years, 9 months ago by alexryans.
    • This reply was modified 3 years, 9 months ago by alexryans.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Straightforward and too the point ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Remove pages from search’ is closed to new replies.