• I have a search filter in my functions.php file to exclude certain pages from the search results. I have two different search forms and on one there is a hidden field and I’m using that to to pass a variable to determine when to exclude the pages. That works well. But what doesn’t work is when you use the search form on a page without the special variable. Right now, that too is excluding pages. I think I am missing something simple but I am not sure what it is. Would love help or a pointer in the right direction.

    Here’s the code from my functions.php file:

    function search_filter($query) {
        $custom = get_query_var('site_search');
      if ( !is_admin() && $query->is_main_query()) {
        if ($query->is_search && $custom = 'visiting') {
            $query->set('post__not_in', array(76,66,49,129,125));
        }
      }
    }
    
    add_action('pre_get_posts','search_filter');

  • The topic ‘Help with search filter to customize page results’ is closed to new replies.