Skip to content
Prem Tiwari
  • WP Tutorials
  • Plugins
  • About Me
  • Featured On
  • Let’s Talk
Prem Tiwari
Home / WordPress Tutorials / How to Exclude Pages from WordPress Search Results

How to Exclude Pages from WordPress Search Results

ByPrem Tiwari Last updated onSeptember 30, 2023

Excluding specific pages or posts from WordPress search results can be useful in various scenarios, such as when you have content that you don’t want to appear in search results. To exclude pages from WordPress search results.

If you prefer not to use a plugin and are comfortable with editing code, you can achieve the same result by adding some custom code to your theme’s functions.php file. Here’s how:

  1. Access Your Theme’s Functions.php:
    • Go to your WordPress admin dashboard.
    • Navigate to “Appearance” > “Theme Editor.”
    • Find and click on the “Theme Functions” (functions.php) file on the right-hand side.

2. Add Custom Code: Add the following code to your theme’s functions.php file to exclude specific pages from search results:

/**
 * Excluding pages from search result.
 */
 
function exclude_pages_from_search() {
    global $wp_post_types;

    $wp_post_types['page']->exclude_from_search = true;
}
add_action( 'init', 'exclude_pages_from_search' );

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

Remember that using custom code requires some technical knowledge, and you should always back up your site before making any changes to your theme files. Using a plugin is generally a safer and more user-friendly option for most WordPress users.

Found it helpful? Share it with your friends on social media!

  • Facebook
  • Post on X
  • LinkedIn
Prem Tiwari

Prem Tiwari is a passionate advocate of open source technology, with over 10+ years of experience in the WordPress domain. He has great experience when it comes to scaled WordPress projects which caters the need for big enterprise clients on WordPress and WordPress VIP-GO platform.

Facebook Twitter Instagram YouTube Linkedin

Post navigation

Previous Previous
Benefits of Removing Website URL Field from the Comment Form in WordPress
NextContinue
How to Increase Maximum Upload File Size in WordPress

Collections

  • Crawling and indexing
  • Meetup
  • PHP
  • Plugins
  • SEO
  • Tech Talks
  • WooCommerce Tutorials
  • WordCamp
  • WordPress Tutorials
  • Privacy
  • Cookies
  • Disable Update Notifications
Facebook Twitter Instagram YouTube Linkedin WordPress
Scroll to top
  • WP Tutorials
  • Plugins
  • About Me
  • Featured On
  • Let’s Talk