How to exclude certain pages from search results

Last edited:
November 5, 2023
Reading time:
3 mins

/

Blog

/

Development

/

How to exclude certain pa...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

Everybody loves searching. WordPress provides a powerful and flexible search feature, allowing visitors to search for content within your website. However, there may be instances when you want to exclude specific pages, such as a “Cookie Policy” or “Privacy Policy” page, from appearing in search results. This article will guide you through the process of excluding certain pages from WordPress search results using a simple code snippet.

Why Exclude Pages from Search Results?

Excluding pages from search results can be beneficial for several reasons:

  1. Privacy Policies and Legal Pages: Pages like “Cookie Policy,” “Privacy Policy,” or “Terms of Service” are essential but may not need to be searchable as they often contain legal text that users can access through links in the footer.
  2. Thank You Pages: Pages that users see after submitting a form, such as a “Thank You” page, typically don’t need to appear in search results.
  3. Landing Pages: Landing pages designed for specific marketing campaigns may not be suitable for general search results.
  4. Custom Content Pages: In some cases, you may have custom pages designed for specific purposes, like membership portals or custom landing pages, which you prefer to keep out of search results.

How to Exclude Pages from Search Results

To exclude specific pages from your WordPress site’s search results, follow these steps:

Step 1: Discover the pages that you want to remove from search results

  1. Log in to your WordPress admin panel.

Step 2: Access Your Child Theme’s Functions.php File

  1. Log in to your WordPress admin panel.
  2. Navigate to “Appearance” and select “Theme Editor.”
  3. In the Theme Editor, you’ll find a list of theme files on the right. Select “Theme Functions (functions.php)” for editing.

Step 3: Add Code to Exclude Pages

Inside the functions.php file, add the following code snippet:

<?php //do not copy this line

function exclude_pages_from_search($query) {
if (!is_admin() && $query->is_search()) {
// Define the page IDs you want to exclude from search results
$excluded_page_ids = array(1, 2, 3); // Replace with the IDs of the pages you want to exclude

// Exclude the specified pages from search results
$query->set('post__not_in', $excluded_page_ids);
}
}
add_action('pre_get_posts', 'exclude_pages_from_search');

In the code snippet, you’ll need to replace 1, 2, 3 with the actual page IDs of the pages you want to exclude from search results.

Step 4: Save Changes

After adding the code, click the “Update File” button to save your changes.

Step 5: Verify Excluded Pages

Now, when visitors search your site, the specified pages will be excluded from the search results. This helps keep your search results relevant and avoids cluttering them with pages that users are unlikely to need in their search queries.

Keep your WordPress search results clean!

Excluding certain pages from WordPress search results is a straightforward process that enhances user experience and search result relevance. By modifying the functions.php file, you can control which pages appear in search results and ensure that important pages, like privacy policies, are still accessible to users through links on your website.

Remember to always back up your theme files before making changes, and be cautious when editing core theme files. With this method, you can provide more focused and relevant search results to your users, helping them find the content they’re looking for with ease.

🚀 Suggested Reading: It wasn’t that hard to exclude unwanted pages from search results. Likewise, it’s easy to add custom fields and meta boxes to your page for using in the front-end.

Leave a Reply

Your email address will not be published. Required fields are marked *