Instead of enumerating page IDs you don’t want, you could instead set the post types that you do want.
You can of course combine the 2 approaches if you wish, but below I’ve included pages, posts, and another type you might want if you have other types on your site.
e.g.:
function tp_remove_pages( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post_type', array( 'page', 'post', 'other_custom_type_you_want' ) );
}
}
add_action( 'pre_get_posts', 'tp_remove_pages' );
EDIT:
An alternative to using this code to modify the search behaviour “after the fact”, is to change the nature of your custom post types when you register them. To do this, use the exclude_from_search
argument when registering your post type.
Ref: https://developer.wordpress.org/reference/functions/register_post_type/
You could also set the public
argument to false
to achieve a similar result, though this may impact other behaviours of your post type.