WordPress Tips

By default, WordPress Search feature displays published posts and published pages in search results.

If you want to show only specific post types into the search query, past the following code into your theme’s functions.php file:

function gioga_search_filter($query) {
	if ( !is_admin() && $query->is_main_query() ) {
		if ($query->is_search) {
			$query->set( 'post_type', array( 'post', 'book', 'service', 'product' ) );
		}
		return $query;
	}
}

add_filter('pre_get_posts','gioga_search_filter');

This filter will show only the post types you insert into the post_type array, excluding all pages.

If you want to exclude only specific pages from the search query, past the following code into your theme’s functions.php file:

function gioga_search_filter($query) {
	if ( !is_admin() && $query->is_main_query() ) {
		if ($query->is_search) {
			$query->set( 'post__not_in', array( 101,156,45,86 ) );
		}
		return $query;
	}
}

add_filter('pre_get_posts','gioga_search_filter');

In the example above, we’re excluding pages with IDs 101, 156, 45 and 86 from the search results. You will need to change these numbers to match the IDs of the pages you want to exclude.

For more filters check Class Reference/WP Query.