Some websites are using specific post types for their content and you might not need all of them to be shown in the search results page. For example if you run a video website and you want your search functionality to show only video results and exclude pages and posts from the query as they are not relevant.

Happily we can use some WordPress filters to do this, and it is quite easy to do it. You would need to edit your functions.php file and add this:

function sow_searchfilter($query) {
 
 if ($query->is_search && !is_admin() ) {
 $query->set('post_type',array('video','gallery'));
 }
 
return $query;
}
 
add_filter('pre_get_posts','sow_searchfilter');

As you can see in the code above, there is this specific line:

$query->set('post_type',array('video','gallery'));

We made sure that in the search results we are including only the following post types: video and gallery. You can add more or stick with only one post type, it’s up to you how to manage this.