Leave pages out of search results
Site search only looks through posts (and products, if you have a shop), not pages like Contact or Privacy policy.
<?php
add_action(
'pre_get_posts',
static function ( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || $query->get( 'post_type' ) ) {
return;
}
$types = get_post_types( array( 'exclude_from_search' => false ) );
unset( $types['page'], $types['attachment'] );
$query->set( 'post_type', array_values( $types ) );
}
);
In these packs
- Search and archives: Better site search and cleaner archive pages.