How to exclude pages from search results

Hi guys,

I’m working on discount coupon website and everything gets indexed fine.
However I can’t seem to figure out how to exclude certain pages.
How do you filer / not display by post_type_label:pages?

Hope somebody can help!

With kind regards

Or how do i exclude: Facets: post_type_label:Blog Manager ?

Hello Bret,

You have two ways to achieve that:

1/ Using our JavaScript Helper, which has the addFacetExclusion, removeFacetExclusion and toggleFacetExclusion methods (See Facet Exclusion). It’s a JavaScript helper so if you’re doing your search from the backend, it might not suit you.

2/ Adding a new boolean field to your record, is_page. And applying a simple facet on is_page:false to exclude those records from your search. This is a simple workaround but could make things simpler for you.

2 Likes

Dear Pixelastic,

Thank you for your reply!
Could you explain me how to add a new boolean field to my record while using a Wordpress website?
Should I add this in database management on my server? or Custom field on my website?

Thank you in advance!

I would also love to know how to implement this. I’m new to Algolia and I’m stumped on how to do this.

Hi there,

Indexing with the Algolia plugin for WordPress is explained here in detail: https://community.algolia.com/wordpress/indexing-flow.html#indexing-decision

Here is a code snippet that should get you started:

<?php
/**
 * @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 );

Let me know if that helps!

1 Like

Thanks rayrutjes. I added that code to my child theme functions.php file, but pages are still showing up in the instant search as well as on the search results page.

Thank you for your reply Rayrutjes! Pasting your snippet worked and I managed to exclude pages from being indexed by Algolia.

I have one remaining question. How does one exclude multiple post types?
For example if I want to exclude both ‘Page’ & ‘Post’

Should I add the snippet twice and change page into post? Or is there a better way to facilitate this?

Your help is much appreciated!

Ahh, I see. I misunderstood and thought the code snippet would prevent the posts in that post type just wouldn’t be displayed. Didn’t realize I had to reindex. That worked to remove the pages from the index.

Although, I also have the same question as dutchinternetmarketi. I need to exclude multiple post types.

Thanks!

1 Like

Can someone help us and explain how to exclude multiple post types?

Hi, Algolia looks amazing but I too am struggling with how to exclude pages from search. I’ve tried WP plugins (I’m not a developer) but no luck. If I assign a WP page a category, how can I tell Algolia to exclude that category in the search results? Or if I use the word ‘download’ in the title or URL, can I do it that way?

I use Yoast, so I saw this explanation (below) – do i just paste this is somewhere?

Another example to exclude posts from the searchable_posts index where the noindex option from the Yoast SEO plugin is set to “noindex”

This example assumes you are using the Yoast SEO plugin for WordPress

<?php
/**
 * Don't index pages where the robot index option
 * in the Yoast SEO plugin is set to noindex.
 *
 * @param bool    $should_index
 * @param WP_Post $post
 *
 * @return bool
 */
function filter_post( $should_index, WP_Post $post )
{
    if ( false === $should_index ) {
        return false;
    }

    return get_post_meta($post->ID, '_yoast_wpseo_meta-robots-noindex', true) == 1 ? false : true;
}

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

Hi @steve.kent,

The shared snippet can be copy pasted in your functions.php file of your active theme.

We offer currently no user interface to select what type of content should or shouldn’t be indexed.

Let me know if copy pasting the code works for you. You will need to trigger a full re-index after making the change to your functions.php file.

Thank you very much! You are an absolute legend.

Hey Rayruthjes,

i have the same issue as @steve.kent but copying the snippet into my functions.php doesnt help. Algolia still indexes posts that have the noindex tag. Any idea?

Hey there,

Could it be that you are talking about posts that display in the autocomplete?
Those are coming from a different index. You need to add the following line to the snippet:

add_filter( 'algolia_should_index_post', 'filter_post', 10, 2 );

Let me know if that works.

Hey rayrutjes,

thanks for your quick reply! Does that work for custom post types too?

Yes that should also work on custom post types.

I added the code to my functions.php but it seems like its not working.
Posts with the custom post type “post_products”, which are set to noindex via Yoast are still visible in the suggestion. :confused:
Another idea?

  1. Can you share your full code?
  2. Also, can you tell if you are trying to hide products from autocomplete or instant search page?
  1. Sure! Im using the following code in the functions.php in a childtheme:

/**

  • Don’t index pages where the robot index option
  • in the Yoast SEO plugin is set to noindex.
  • @param bool $should_index
  • @param WP_Post $post
  • @return bool
    */
    function filter_post( $should_index, WP_Post $post )
    {
    if ( false === $should_index ) {
    return false;
    }
return get_post_meta($post->ID, '_yoast_wpseo_meta-robots-noindex', true) == 1 ? false : true;

}

// Hook into Algolia to manipulate the post that should be indexed.
add_filter( ‘algolia_should_index_searchable_post’, ‘filter_post’, 10, 2 );
add_filter( ‘algolia_should_index_post’, ‘filter_post’, 10, 2 );

  1. Im trying to hide them from the autocomplete, because there is now value behind it, as some of the pages are blank.

The website is www.gintlemen.com. When u search for “barsieb” for example, you will see what i mean. Those are fake products, that should not be shown within the autocomplete.