Yes that works for the search result page but not for the ajax hints in the top menu search. The page is still suggested…
But the plugin works.
// Exclude specific posts/pages from search
// https://www.wpexplorer.com/limit-wordpress-search/
function exclude_pages_from_search($query) {
if ( $query->is_main_query() && is_search() ) {
$exclude_ids = array( 266, 156 ); // Array of the ID’s to exclude
$query->set( ‘post__not_in’, $exclude_ids );
}
return $query;
}
add_filter( ‘pre_get_posts’,’exclude_pages_from_search’ );