• 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.