How to exclude pages from search results

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.