Exclude Specific Pages From Search
Hides chosen posts or pages (thank-you pages, landing pages, legal pages) from front-end search results.
Opens WPIDE on
Code
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() ) {
return;
}
// IDs of the posts or pages to hide.
$excluded = array( 0 );
$query->set( 'post__not_in', array_merge( (array) $query->get( 'post__not_in' ), $excluded ) );
} );
Notes
Replace 0 with the IDs to hide.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.