WordPress.org

Ready to get started?Download WordPress

Forums

search filter function (1 post)

  1. sahdow
    Member
    Posted 2 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic