How to exclude pages from search results

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.

No idea? I really need some help with that! :smiley:

Hi, I also need help with this. After applying the code above (on a website using https://themeforest.net/item/startit-a-fresh-startup-business-theme/13542725), non-indexed pages were hidden from search results. Now they’ve reappeared (in the search results page, I don’t have any results appearing in the search bar as users type). I’ve tried reindexing, pushing, but no luck. Can you please confirm this is still the right code? Thanks

I have the same problem.
In my dashboard I have 2 indices (wp_searchable_posts and wp_post_page). And the page with noidex is not in the first one (wp_sercheable_post), but appears in the second one (wp_post_page). And my problem is that it still appears in the autocomplete.
I’m using the same code as Christoph.
Cheers!

Hi Algolia would really appreciate a reply on this, otherwise will have to remove Algolia from all my websites, which would be a massive disappointment.

I’ll take a stab at answering your question. In this code, look at the line that begins with $excluded_post_types. It accepts an array which means you can list multiple values in there. Let’s say you have a custom post type for careers and people and you don’t want either of these to show up in search. I havent tested this but you can probably do something like this for custom post types. Add that to your functions.php in your active theme and then re-index. It should remove the careers and people custom post types from the search results.

function exclude_post_types( $should_index, WP_Post $post )
{
    // Add all post types you don't want to make searchable.
    $excluded_post_types = array( 'careers', 'people' );
    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 );

IF these custom post types are post types that you registered yourself in your theme, you can also easily remove them the search results by setting the value exclude_from_search => true in the register_post_type code in your functions file. I actually had to use both approaches today. I have several custom post types that I didnt want in the results that I had created myself. I added that line and re-indexed. They are now gone from my search results.

For example, if you have a post type called People that you registered in your theme you can add the exclude_from_search here like this:

register_post_type( 'people', // change the name
	array(
	  'labels' => array(
	  'name' => __( 'People' ), // change the name
	  'singular_name' => __( 'people' ), // change the name
	),
	'public' => true,
    'exclude_from_search' => true, // make this not searchable
	'supports' => array ( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail' ), // do you need all of these options?
	'hierarchical' => true,
	'menu_icon' => get_bloginfo( 'template_directory' ) . "/images/icon.png",
	'rewrite' => array ( 'slug' => __( 'about-us/people' ) ) // change the name
	)
);
1 Like

Wow thank you for the reply :slight_smile: Sorry I’ve been away so just seen your message, much appreciated. I’ll try this and let you know how it goes, just wanted to say thanks first! Would this work for pages as well?

hi

how do i use this to exclude certain post title?
my site is at https://autovacbot.com, when i tried to search for “roomba” without the quotes, i can see one of my product with the title (Discontinued) appears.

how do i remove products with “Discontinued” from the result?

i also have a badge plugin added to overlay a discontinued badge over the product. and i have added to the indices as well.