How To Exclude Posts, Pages and More From WordPress Search Results

In this tutorial, I will show you how to refine and filter Search Results in your WordPress Blog´s front-end. Specifically, I will show you how to Exclude Posts, Pages, specific Categories, Custom Post Types and more from your WordPress Search Option.

In general, every WordPress Blog has a Search box that you can use to search based on your keywords. Sometimes you don´t want to display every post and page in your blog´s search results. You want to filter the Search to exclude certain results.

It is important to note that filtering Search results is limited to your Blog (front-end). The search in your dashboard (back-end) is unaffected by the Code. The Search Box for one of the theme´s of this Blog is shown below.

Search Box in a WordPress Blog.

Here is How To …

1. Exclude ALL Pages From WordPress Search Results

By default, WordPress Search feature search in ALL published posts and pages along with attachments and Custom Post Types. In this section, I will show you how to make your search less crowded by excluding ALL pages (along with Attachments and CPTs) from search results. To do that …

Open functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-1. Save the file and upload it to the server.

CODE-1:

<?php
/****************************CODE-1**********************************
* @Author: Boutros AbiChedid 
* @Date:   June 6, 2012
* @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
* @Description: Exclude ALL pages, Custom Post Types and Attachments 
* from WordPress Search Results.
* Function that Searches In Posts Only.
* @Tested on: WordPress version 3.3.2 
*********************************************************************/
function bac_filter_wp_search($query) { 
	//Filter Search only in the front-end and not in the Admin.
	if (!$query->is_admin && $query->is_search) {
	//Search in Posts only. Excludes Pages, Attachments and CPTs.
	$query->set('post_type', 'post');
	}
	return $query;
}
add_filter('pre_get_posts','bac_filter_wp_search');
?>

CODE-1 Notes:

Line 13 of CODE-1: Also checks not to affect searches in your WordPress dashboard (Admin side). This code will filter only searches done on the front-end of the Blog.

CODE-1 searches for posts ONLY by setting the ´post_type´ to ´post´. You can also do the opposite by setting the ´post_type´ to ´page´, so it only returns pages in the search result.

CODE-1 executes searches in Posts only. The code excludes ALL Pages, Attachments and Custom Post Types.

CODE-1 References:

2. Exclude ALL Pages But Include Custom Post Types In WordPress Search Results

So how to include Custom Post Types in your searches?

CODE-1 excludes not only pages from search results, but also other Custom Post Types. So if you have CPTs in your WordPress Blog, and you want to include them to your searches. Then use CODE-2 below.

Open functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-2. Save the file and upload it to the server.

CODE-2:

<?php
/****************************CODE-2**************************************
* @Author: Boutros AbiChedid 
* @Date:   June 6, 2012
* @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
* @Description: Exclude ALL pages and Attachments from WP Search Results.
* Function searches In ALL Posts and Custom Post Types Only.
* @Tested on: WordPress version 3.3.2 
*************************************************************************/
function bac_filter_wp_search($query) { 
	//Filter Search only in the front-end and Not in the Admin.
	if (!$query->is_admin && $query->is_search) {
	//Search in Posts and CPTs only. Excludes Pages and Attachments .
	$query->set('post_type', array('post', 'your-CPT-name-here'));
	}
	return $query;
}
add_filter('pre_get_posts','bac_filter_wp_search');
?>

CODE-2 Notes:

You can use CODE-1 OR CODE-2 but NOT both.

CODE-2 will return results from ALL posts and from Custom Post Type called ´your-CPT-name-here´. You should replace that with your Custom Post Type name.

Line 12 of CODE-2: Also checks not to affect searches in your WordPress dashboard (Admin side). This code will filter only searches done on the front-end of the Blog.

If you have several CPTs that you want to include in your WordPress Search Results along with your Posts. Then, separate them by a comma. For instance if you have 2 CPTs, then replace Line 14 of CODE-2 with the following:

//Search in ALL Posts and 2 CPTs. Separate them by commas.
$query->set('post_type', array('post', 'your-CPT-name-here1', 'your-CPT-name-here2' ));

If you want to exclude a Custom Post Type from search results then add exclude_from_search => true, when you register your post type.

3. Exclude SPECIFIC Pages and Posts From WordPress Search Results

If you want to Exclude specific pages or posts, such as “Thank You” page or “Hello” post, from your WordPress internal search. Then use CODE-3 below.

Open functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-3. Save the file and upload it to the server.

CODE-3:

<?php
/****************************CODE-3****************************
* @Author: Boutros AbiChedid 
* @Date:   June 6, 2012
* @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
* @Description: Exclude Certain Pages and Posts by ID. 
* @Tested on: WordPress version 3.3.2 
**************************************************************/
function bac_filter_wp_search($query) { 
	//Filter Search only in the front-end and Not in the Admin.
	if (!$query->is_admin && $query->is_search) {
	//Excludes specific Pages and Posts by ID. Separated by comma.
	$query->set('post__not_in', array(1,1158));
	}
	return $query;
}
add_filter('pre_get_posts','bac_filter_wp_search');
?>

CODE-3 Notes:

Line 11 of CODE-3: Also checks not to affect searches in your WordPress dashboard (Admin side). This code will filter only searches done on the front-end of the Blog. The search in the Back-end is NOT affected.

Line 13 of CODE-3: The number 1 is a page ID on this blog, and the the number 1158 is a post ID on this blog. You need to replace these numbers with yours. If you don´t know how to find the Page ID or Post ID, then read my previous tutorial on How To Find the Page and Post ID.

On line 13 of CODE-3: You can exclude as many pages as you want, just add the IDs to the array separated by commas. You can also use this code to exclude child-pages (or SubPages). You can also exclude posts by ID.

4. Exclude Certain Categories From WordPress Search Results

If you don´t want all your categories to appear in your blog´s search results. Then use CODE-4 below.

Open functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-4. Save the file and upload it to the server.

CODE-4:

<?php
/****************************CODE-4****************************
* @Author: Boutros AbiChedid 
* @Date:   June 6, 2012
* @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
* @Description: Exclude Certain Categories by ID. 
* @Tested on: WordPress version 3.3.2 
**************************************************************/
function bac_filter_wp_search($query) { 
	//Filter Search only in the front-end and Not in the Admin.
    if (!$query->is_admin && $query->is_search) {
	//Excludes specific Categories by ID. Separate by comma.
	$query->set('cat','-143,-294');
	}
	return $query;
}
add_filter('pre_get_posts','bac_filter_wp_search');
?>

CODE-4 Notes:

Line 11 of CODE-4: Also checks not to affect searches in your WordPress dashboard (Admin side). This code will filter only searches done on the front-end of the Blog. The search in the Back-end is NOT affected.

Line 13 of CODE-4: The numbers 143 and 294 are two category IDs on this blog, you need to replace these number with yours. If you don´t know how to find the Category ID, read my previous tutorial on How To Find the category ID.

Line 13 of CODE-4: the minus sign excludes the specific categories (e.g. -143 exclude all posts having the Category ID of 143). The minus sign is important. If you don´t add it, the search will show ONLY results under category ID of 143.

What will happen if a post is assigned to multiple categories Even if you assign a post under several categories, when one of the categories is excluded from the Search, the post will NOT show in the search results.

5. Exclude All Pages AND Some Posts by Categories from WordPress Search Results

If you want to search in Posts Only BUT you want to Exclude Specific Posts by category ID. Meaning, you want to exclude from WordPress search results all pages, attachments and CPTs and also few posts based on their categories. In this case you need to combine CODE-1 AND CODE-4 and not paste them as separate blocks as shown in CODE-5.

Open functions.php file located in your theme’s folder, and add (copy and paste) the following CODE-5. Save the file and upload it to the server.

CODE-5:

<?php
/****************************CODE-5****************************************
* @Author: Boutros AbiChedid 
* @Date:   June 6, 2012
* @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
* @Description: Search in Posts Only BUT Exclude Certain Posts by Category. 
* Also Exclude Pages, Attachments and CPTs.
* @Tested on: WordPress version 3.3.2 
***************************************************************************/
function bac_filter_wp_search($query) { 
	 //Filter Search only in the front-end and Not in the Admin.
     if (!$query->is_admin && $query->is_search) {
	 //Search in Posts only. Excludes Pages, Attachments and CPTs.
     $query->set('post_type','post');
     
     //But Exclude certain Posts belonging to a specific Category ID.
	 $query->set('cat','-143');
	}
	return $query;
}
add_filter('pre_get_posts','bac_filter_wp_search');
?>

CODE-5 Notes:

Here I gave you an example on how to merge different Code Blocks. You can do the same for other combinations. For example, if you only want to Search in Posts but Exclude certain posts by their IDs instead of by their categories. Then, you can replace lines 16 and 17 by the following:

//But Exclude a Post by its ID. Note that 1 and 32, are two post IDs on this blog, replace them with yours.
$query->set('post__not_in', array(1, 32));

Line 17 of CODE-5: The number 143 is a category ID on this blog, you need to replace this number with yours.

Line 17 of CODE-5: the minus sign excludes the specific categories (e.g. -143 exclude all posts having the Category ID of 143). The minus sign is important. If you don´t add it, the Search will show ONLY results under category ID of 143.

Line 12 of CODE-5: Add a check to make sure that search in the Back-end is NOT affected.

Your Turn to Talk

In this tutorial, I showed you different Code blocks to filter Search Results in you WordPress blog based on your needs. There are other possibilities, but hopefully you have a solid starting point.

If you have something to add, or anything else to say, please share it in the comment section. Your opinion matters, unless it is a Spam.

If you found this post useful, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader, or feel free to donate. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

6 Responses to “How To Exclude Posts, Pages and More From WordPress Search Results”

  1. Stephen says:

    pleasure

  2. Stephen says:

    Just pages. I found something:

    function remove_posts_from_wp_search($query) {
    if ($query->is_search) {
    $query->set('post_type', 'page');
    }
    return $query;
    }
    add_filter('pre_get_posts','remove_posts_from_wp_search');
    
  3. Stephen says:

    How do I exclude all posts from wordpress search?

  4. DG says:

    I have a little question for CODE-1:

    That code will work for search posts only and exclude for “ALL Pages”, “Custom Post Types” and “Attachments” in WordPress search results, I got it.

    But, is it possible to exclude only really for “All Pages”? (Posts, Attachments, Custom Post Type are included) Because that code not only works well on WordPress search results on the site (like the screenshot above), at the Dashboard > Media (search column), I also can’t search specific images because the CODE-1.

    Should I use the CODE-3 or CODE-5 to solve my problem? Or you might have another tips?

    Thanks for the reply. I really love all your WordPress posts.