CMS Scripts

Exclude pages from search results in WordPress

Exclude pages from search results  in WordPress

The research shows the results, including WordPress pages. A quick way to solve this problem is to remove all pages from the search results. The function can be expanded with the addition of other types of post_type for the query. File to edit: functions.php

 

PHP
1
2
3
4
5
6
7
function filter_search($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘page’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘filter_search’);

Leave a Reply