How to exclude only some pages/posts from your search results

While you can easily exclude all pages at once from displaying in the search results, chances are that you would probably need at least one page to stay there. It doesn’t matter if it’s only one page, two or ten which you want to exclude from showing in the search results, you can do that easily by implementing the code.

Before you rush copy-pasting the code into your functions.php file, we suggest you prepare the pages which you don’t want to be featured in the search results. You will need page IDs:

  1. Go to Pages->All Pages
  2. Find the page you want to exclude and click “Edit”
  3. In the address bar, locate the “post=6” or something similar where the number will represent a page ID
  4. After you have prepared pages’ ID numbers, you are ready for the code:

  5. Go to function.php file and open it
  6. Copy and paste the following code:
  7. function jp_search_filter( $query ) {
    if ( $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 1,2,3,4,5,6 ) );
    add_action( 'pre_get_posts', 'jp_search_filter' );
  8. In line #3, change the numbers in the brackets to ID numbers which you have read from page. In this example, we have excluded 6 pages with their respective IDs 1 to 6.
  9. Save changes and you’re done

After you have installed the code, you are free to edit it as needed. If you need to include one of the pages sometimes in the future, simply delete its ID number from the code. Same goes for adding more pages into the “excluded basket”. If you, for example, create a new page which you don’t want to show in the search results, feel free to add its ID number in the code.


This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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