Title: search filter function
Last modified: August 20, 2016

---

# search filter function

 *  [sahdow](https://wordpress.org/support/users/sahdow/)
 * (@sahdow)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/search-filter-function/)
 * I’m trying to write a function for my theme’s functions.php that will exclude
   specific custom post types from the WordPress search.
 * Example: this excludes the custom post type, however it also excludes all results
   from the search.
 *     ```
       function SearchFilter($query) {
            if ($query->is_search) {
               $query->set('post_type', '-customposttype');
            }
            return $query;
       }
       add_filter('pre_get_posts','SearchFilter');
       ```
   
 * If the – is removed, the search only returns the custom post type.
 * When I use the almost identical filter to remove posts in a specific category
   no problems.
 *     ```
       function SearchFilter($query) {
            if ($query->is_search) {
               $query->set('cat','-4') ;
            }
            return $query;
       }
       add_filter('pre_get_posts','SearchFilter');
       ```
   
 * Any ideas on what I’m doing wrong?

The topic ‘search filter function’ is closed to new replies.

## Tags

 * [exclude](https://wordpress.org/support/topic-tag/exclude/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [function](https://wordpress.org/support/topic-tag/function/)

 * 0 replies
 * 1 participant
 * Last reply from: [sahdow](https://wordpress.org/support/users/sahdow/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/search-filter-function/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
