Sometimes you don’t want all of your posts and pages appearing in your search results. Use this snippet to shun whichever ones you want.
// search filter function fb_search_filter($query) { if ( !$query->is_admin && $query->is_search) { $query->set('post__not_in', array(40, 9) ); // id of page or post} return $query; } add_filter( 'pre_get_posts', 'fb_search_filter' );
To exclude the subpage of a page you need to add it to the IS:
// search filter function fb_search_filter($query) { if ( !$query->is_admin && $query->is_search) { $pages = array(2, 40, 9); // id of page or post// find children to id>foreach( $pages as $page ) { $childrens = get_pages( array('child_of' => $page, 'echo' => 0) ); } // add id to array>for($i = 0; $i < sizeof($childrens); ++$i) { $pages[] = $childrens[$i]->ID; } $query->set('post__not_in', $pages ); } return $query; } add_filter( 'pre_get_posts', 'fb_search_filter' );