Exclude Pages from WordPress Search Results (PHP)

Estimated reading: 4 minutes 1005 views

Want to exclude pages or other post type from WordPress search results? By using a simple code snippet, you can ensure no one sees those private pages when a search has been created. Maybe there’s something you don’t want readers to bump into? Well, this code will definitely take care of this concern. Here’s how to get started:

Code Snippets

Here are some great snippets to add to your website, choose which is best for your setup or create your own post type.

Want To Allow Posts Only?

This function modifies the main WordPress query to remove pages, products such as SureCart and other custom types from search results:

function tg_exclude_pages_from_search_results( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post' ) );
    }    
}
add_action( 'pre_get_posts', 'tg_exclude_pages_from_search_results' );

Want To Allow SureCart?

This is the function we use because we use SureCart, it is specific for excluding pages from search results, but keeps SureCart:

function exclude_pages_but_keep_surecart($query) {
    if ($query->is_search && $query->is_main_query() && !is_admin()) {
        // Keep posts + SureCart pages
        $query->set('post_type', array('post', 'sc_product'));
    }
}
add_action('pre_get_posts', 'exclude_pages_but_keep_surecart');

Want To Allow WooCommerce?

This function is specific for excluding pages, but keeps blog posts and WooCommerce products searchable:

function tg_exclude_pages_from_search_results( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post', 'product' ) );
    }    
}
add_action( 'pre_get_posts', 'tg_exclude_pages_from_search_results' );

Want to exclude a particular page?

Add this code underneath the other query modification, and change the 112 for the ID of the page you want to exclude from your search results. You will need to head over to the post you want to exclude, open it, check the URL for its ID and replace the ID in our code.

$query->set( 'post__not_in', array( 112 ) );

Want to exclude a category?

Add this code underneath the other query modification and where 11 and 12 are the category IDs, it’s suggesting they are excluded from the query. You will need to head over to the category you want to exclude, open it, check the URL for its ID and replace the ID in our code.

$query->set( 'cat', '-11, -12' );

How To Create Your Own Post Types

ou can also create your own custom search functions by adjusting the array of post types. To find out which post type you need to include, you can right-click on the page in question, choose Inspect (the wording may vary depending on your browser), and explore the Dev Tools. Another option is to install a free plugin like Show Current Template or use Query Monitor, both of which will display the page’s Post Type so you can add it to your array.

  • array( 'post' ) → only blog posts are searchable.
  • array( 'post', 'product' ) → blog posts + WooCommerce products are searchable.
  • array( 'post', 'product', 'sc_product' ) → blog posts + products + SureCart pages are searchable.

Where to Add This Code

Here are two easy options:

Option 1: Install WPCode Lite Plugin

This is by far the safest method and is beginner-friendly if you’re not used to editing theme files.

  1. Search for WPCode Lite plugin in your website dashboard.
  2. Install the plugin.
  3. Go to Snippets > Add New.
  4. Give your snippet a name like “WordPress Search Results
  5. Paste the code above.
  6. Choose “Run snippet everywhere”.
  7. Choose PHP, not CSS or any other.
  8. Save and activate.

 ✅ Done!

Option 2: Functions.php

We don’t advise this option unless you’re a developer or if you feel comfortable editing theme files:

  1. Open your functions.php file.
  2. Paste the snippet at the bottom.
  3. Save and upload if needed.

 Tip: Always use a child theme to avoid losing changes when your theme updates.

NOTE: We use WPCode Lite because it’s more flexible than others, there’s no need to upgrade to pro unless you’re a developer, it offers many free snippets, can choose where to run it, won’t publish if the code is wrong so won’t break your site, PHP is included compared to others you have to buy their pro, and best of all the free version can easily be downloaded from your WordPress Repository.

Leave a Reply

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

Share this Doc

Exclude Pages from WordPress Search Results (PHP)

Or copy link

CONTENTS