This snippet removes selected pages from all WordPress search results. Replace the sample IDs with your page IDs (find them by hovering over a page title in the Pages list or use a plugin like “Show IDs”). Once added, those pages will no longer appear in searches sitewide, which is useful if you want to hide landing pages, thank-you pages, or other non-public content from showing up in results.
Place this code in your theme’s functions.php or add it with a snippets plugin.
function exclude_pages_search( $query ) {
if ( $query->is_search() && ! is_admin() ) {
$query->set( 'post__not_in', array( 12, 34, 56 ) ); // page IDs
}
}
add_action( 'pre_get_posts', 'exclude_pages_search' );
Some solutions only work on the live site. Always publish and test after each change, as results may not appear in the editor.