• When a user visits an archive.php with post_type ‘boats’ AND they use the search function, I’d like to restrict the search results to display only results of ‘boats’. Make Sense?

    I can display my post_type properly. I can even set the filter….

    add_filter('pre_get_posts','SearchFilter');
    
    function SearchFilter($query) {
      if ($query->is_search) {
        // Insert the specific post types you want to search
        $query->set('post_type', array('boats'));
      }
      return $query;
    }

    I am trying the following code…..

    if( get_post_type() == 'boats') {
        add_filter('pre_get_posts','SearchFilter');
       }

    I think that “pre_get_posts” is run before “get_post_type” so this will always fail. How can I restrict search results only while searching from said post_type?

  • The topic ‘Restrict search results to post_type only while on post_type archive’ is closed to new replies.