Search
Close this search box.

How to Exclude Specific Pages from Search Results Page in WordPress (without Plugin)

WordPress search results encompass a wide array of content on your site, including posts, custom post types, pages, categories, and tags. However, certain pages may not be directly relevant to search queries, potentially hindering user experience. Visitors often seek specific posts rather than general pages.

To optimize user experience, it is beneficial to exclude irrelevant pages such as the privacy policy, contact, and about pages from search results. This ensures that users quickly find the information they need, streamlining their interaction with your site.

Moreover, excluding specific pages offers an added layer of privacy and security. If your site contains pages with sensitive or private information that should not be easily accessible through search engines, excluding them enhances confidentiality.

If you prefer to achieve this without relying on plugins, the following article provides guidance on excluding specific pages from search results using the URL ‘slug’ of those pages through custom code. Let’s delve into the details!

How to Exclude Specific Pages from Search Results Page in WordPress

Get the Custom Code Ready

To exclude specific pages from search results without a plugin, you can add the following custom code on your theme’s functions.php file which is the approach we’ll be taking in this tutorial, or create your own custom plugin with the code.

Here’s an example of how you can modify the code to exclude specific pages like ‘about’ and ‘privacy-policy’:

function wpp_search_filter( $query ) {
    if ( $query->is_search && ! is_admin() ) {
        // Exclude specific page IDs by slug
        $exclude_pages = array( 'about', 'privacy-policy' );

        $exclude_page_ids = array();
        foreach ( $exclude_pages as $page_slug ) {
            $page = get_page_by_path( $page_slug );
            if ( $page ) {
                $exclude_page_ids[] = $page->ID;
            }
        }
       
        if ( ! empty( $exclude_page_ids ) ) {
            $query->set( 'post__not_in', $exclude_page_ids );
        }
    }

    return $query;
}
add_filter( 'pre_get_posts', 'wpp_search_filter' );

In this example, we create an array called $exclude_pages where you can specify the slugs of the pages you want to exclude. The function then retrieves the corresponding page IDs based on the slugs and sets the post__not_in parameter to exclude those pages from the search results.

Make sure to replace ‘about’ and ‘privacy-policy’ with the actual slugs of the pages you want to exclude. You can also add more pages to exclude by adding the page slug to the $exclude_pages array.

Implement the Custom Code

After finalizing your custom code, proceed to implement it by adding the code to the functions.php file. To do this, navigate to Appearance on your WordPress dashboard and select Theme File Editor from your WordPress dashboard menu. Click on functions.php within the Theme Files, and insert the custom code at the bottom of the file.

If you’re working with a block theme, you can find the Theme File Editor menu located within the Tools menu. In the case that your chosen theme, like “Twenty Twenty Three,” doesn’t include the functions.php file, you might need to create it via SFTP or your site’s file manager initially.

Once you’ve added the code, click the Update File button to implement the code.

Upon implementing the exclusion of certain pages, if you use the WordPress search engine with keywords present in your about or privacy policy page, those specific pages will no longer appear in the search results. The following illustration depicts the contrast between the search results before and after excluding these pages.

Before
After

The Bottom Line

Excluding specific pages from WordPress search results based on their URL ‘slug’ not only improves user experience by refining content discovery but also fortifies privacy and security.

This tailored approach ensures that visitors swiftly locate pertinent information, fostering a more efficient and secure interaction while safeguarding sensitive content from unnecessary exposure.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Picture of Hendri Risman

Hendri Risman

Hendri is a WordPress expert and a writer staff at WPPagebuilders. He writes solutions on how to get things fixed in WordPress a lot. Mostly without involving a plugin.
Want to turn your WordPress knowledge into revenue? OF COURSE!

Leave a Comment

Share This
Hey 👋🏻
Do you have a WP blog?
If so, you may need these. All the resources you need to grow your blog.

Your popup content goes here

50%

Where should we send the template?

After entering your email address, you will be added to our newsletter subscribers. You can unsubscribe anytime.

Want to Build Passive Income Like the One on the Screenshot Below?

Click the button on the right side to learn how 👉🏻
5 easy steps to turn your WordPress knowledge into monthly recurring revenue.

Click the above button to close the popup.