This snippet hides selected pages from WordPress search results by using their page IDs.
This is useful for hiding thank-you pages, private landing pages, or any content you want excluded from search results while still accessible via direct link.
Copy & paste the scripts before the </body> tag of your project. If you added them before for another setup, skip this step.
Right-click in Elementor, choose “Paste from another site,” and while the popup is open, press cmd/ctrl + v to insert the layout.
Place the code in an HTML widget or add it through Elementor → Custom Code (before the closing </body> tag) either globally or only on selected pages.
Paste the code through the page or site settings, or add it via Elementor → Custom Code (before </body>) for broader use.
Paste the script through Elementor → Custom Code (set to load after </body>) for site-wide or page-specific loading.
Place the PHP snippet inside your theme’s functions.php or using any code snippet to enable logic.
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' );To find a post or page ID, go to Pages (or any post type) in your WordPress dashboard and hover over an item. The ID appears in the browser link, usually as post=123.
Replace the example numbers in the code with those IDs.
You can also use a plugin like Show IDs to display IDs directly in the admin list.