WordPress has a built-in search feature to make it easy for visitors to find certain content on a website. By default, the feature displays all content types. Including pages. If you want to exclude pages from the results page, this post will show how.
As the title suggests, I will show you how to exclude pages from the results page manually, without using a plugin. So, custom code will be involved here.
In other words, you need to be familiar with adding a custom function.
If you already know how to add a custom function in WordPress, you can jump to this section to get the code snippet. Otherwise, you can continue below to learn the steps to exclude pages from the search results page via custom code.
Shortcuts โคต๏ธ
When Do You Need to Exclude Pages from Search Results Page on Your WordPress Site?
While you can use WordPress to create any type of website (including static sites), it is a CMS by nature. With WordPress, you can create a content-heavy site like a blog, news, forum and so on.
And to make it easy for your site’s visitors to find certain content on your website, you can provide a search feature which you can place to the site header or anywhere per your preference.
As I mentioned above, the default WordPress configuration shows all content types on the search results page. Including pages, which is not ideal.
You definitely don’t want pages like Privacy page and Term of Service page to show up on the search results page. Don’t you?
That’s why you want to exclude them.
Excluding Pages from WordPress Search Results Page
As usual, there are two options to achieve a thing in WordPress. First, you can use a plugin. Second, you use custom code.
Since we want to achieve this particular thing via custom code, then you don’t need to install a new plugin on your WordPress site. Instead, you can use one of the following code snippets:
Excluding All Pages
if (!is_admin()) {
function search_filter_pages($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','search_filter_pages');
}
The above code snippet instructs WordPress to display only the default post type (a.k.a blog post) on the search results page when a new search query is performed.
With the above code snippet, WordPress will ignore all content types (including pages) other than blog posts.
One crucial thing. The above code snippet will exclude all pages from the search results page.
Excluding Specific Pages
If you want WordPress to exclude specific pages instead all of pages from the results page, you can use the following code snippet:
if (!is_admin()) {
function search_filter_exclude_pages_by_slug($query) {
if ($query->is_search) {
$excluded_slugs = array('about-us', 'privacy-policy'); // Add your page slugs here
$excluded_page_ids = get_posts(array(
'fields' => 'ids',
'post_type' => 'page',
'post_name__in' => $excluded_slugs,
'numberposts' => -1,
));
if (!empty($excluded_page_ids)) {
$current_excluded = (array) $query->get('post__not_in');
$query->set('post__not_in', array_merge($current_excluded, $excluded_page_ids));
}
}
return $query;
}
add_filter('pre_get_posts', 'search_filter_exclude_pages_by_slug');
}
The above code snippet has the same functionality as the first code snippet. But this time, it only excludes pages with the slug about-us
and privacy-policy
.
In other words, it only excludes the About Page and the Privacy Policy page, assuming these pages have slugs about-us
and privacy-policy
.
You can modify the fourth line of the above code snippet and replace the page slugs with the actual page slugs that you want to exclude from the search results page on your website.

To exclude more pages, you can simply add the slugs of the pages you want to exclude on the same line. Make sure to add a comma to separate each page.

How to Add the Code
I have provided two code snippets which you can use to exclude all pages and specific pages from the WordPress search results page.
The question is, how to add them?
To add the code snippets above, you can simply edit the functions.php file of your active theme. To find this file, login to your WordPress dashboard and go to Appearance -> Theme File Editor.

๐จ If you are a block theme user, you can find Theme File Editor under the Tools menu.
On the Edit Themes screen, select the functions.php file on the right panel to edit it. Paste the code snippet you want to use right after the last line of the file content and click the Update File button.

That’s it. Your desired pages should now be excluded from the search results page.
Another Option to Add the Code
Editing the functions.php file of the active theme is the easiest way to add a custom function in WordPress. Unfortunately, it is also the least effective way.
Why?
Because every time you update your theme, you need to re-add the custom code.
If you want to add a custom function that works permanently, you can create a plugin dedicated to hosting custom functions on your website. In the future, every time you want to add a new custom function, you can simply edit the file of the plugin.
If you are interested in using the method, I have published the detailed instructions that you can read.
๐จ Bonus: Excluding Custom Post Types from WordPress Search Results Page
The first code snippet above will filter the search results to only display blog posts on your website. Meaning that other post types will be excluded. Including page and custom post type.
What if you want to exclude custom post types instead?
If you run an LMS website or membership website in which the content is a custom post type, then excluding it from the search results page, while keeping the blog posts and pages searchable, is more rational.
To exclude custom post types from the WordPress search results page, you can modify the first code snippet. More precisely, you can edit the fourth line into something like this:

So, your final code will look like this:
if (!is_admin()) {
function search_filter_pages($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'page'));
}
return $query;
}
add_filter('pre_get_posts','search_filter_pages');
}
The above code will filter the search results to display only blog posts and pages. Meaning that custom post types will be excluded.
Summary
While the built-in search feature of WordPress is useful enough for content-heavy sites like a blog or a news site, its default configuration may not impress you. Mainly because it displays everything you have published. Including pages like the Privacy Policy page and the Terms of Service page which are supposed to be excluded.
You can use the code snippets above to exclude either all pages or specific pages from the search results page on your WordPress site.
If you want a permanent page exclusion, you can add the code by creating a custom plugin. If you are okay with a temporary page exclusion, you can simply edit the functions.php of your active theme.