Skip to main content
added 72 characters in body
Source Link
Rarst
  • 99.9k
  • 10
  • 160
  • 298

TIA

add_filter('pre_get_posts','SearchFilter'); function SearchFilter($query) { if( ! is_admin() ) { if ($query->is_search) { $aPT = array(); if ( isset($_GET['include_docs']) ) $aPT[] = 'docs'; if ( isset($_GET['include_general']) ) { $aPT[] = 'post'; $aPT[] = 'page'; } if ( isset($_GET['include_nl']) ) $aPT[] = 'newsletter'; $query->set('post_type', $aPT ); $post_type = get_query_var('post_type'); if($post_type) $post_type = $post_type; else $post_type = $aPT;
        $query->set( 'cat', '-7,-19' )  // should only apply to posts not cpts
    }

} return $query;

add_filter('pre_get_posts','SearchFilter');
function SearchFilter($query) {
  if( ! is_admin() ) {
        if ($query->is_search) {
            $aPT = array();             
            if ( isset($_GET['include_docs'])  )
               $aPT[] = 'docs';    
            if ( isset($_GET['include_general']) ) {
               $aPT[] = 'post';
               $aPT[] = 'page';                
            }
            if ( isset($_GET['include_nl']) )
               $aPT[] = 'newsletter';              
            $query->set('post_type', $aPT );    
            $post_type = get_query_var('post_type');
            if($post_type)
               $post_type = $post_type;
            else
               $post_type = $aPT;

            $query->set( 'cat', '-7,-19' )  // should only apply to posts not cpts
        }
  }
 return $query;  

TIA

add_filter('pre_get_posts','SearchFilter'); function SearchFilter($query) { if( ! is_admin() ) { if ($query->is_search) { $aPT = array(); if ( isset($_GET['include_docs']) ) $aPT[] = 'docs'; if ( isset($_GET['include_general']) ) { $aPT[] = 'post'; $aPT[] = 'page'; } if ( isset($_GET['include_nl']) ) $aPT[] = 'newsletter'; $query->set('post_type', $aPT ); $post_type = get_query_var('post_type'); if($post_type) $post_type = $post_type; else $post_type = $aPT;
        $query->set( 'cat', '-7,-19' )  // should only apply to posts not cpts
    }

} return $query;

add_filter('pre_get_posts','SearchFilter');
function SearchFilter($query) {
  if( ! is_admin() ) {
        if ($query->is_search) {
            $aPT = array();             
            if ( isset($_GET['include_docs'])  )
               $aPT[] = 'docs';    
            if ( isset($_GET['include_general']) ) {
               $aPT[] = 'post';
               $aPT[] = 'page';                
            }
            if ( isset($_GET['include_nl']) )
               $aPT[] = 'newsletter';              
            $query->set('post_type', $aPT );    
            $post_type = get_query_var('post_type');
            if($post_type)
               $post_type = $post_type;
            else
               $post_type = $aPT;

            $query->set( 'cat', '-7,-19' )  // should only apply to posts not cpts
        }
  }
 return $query;  
Source Link
jchwebdev
  • 775
  • 2
  • 14
  • 33

Search pre_get_posts filter which can handle multiple post types and categories?

I have a Search which allows users to search multiple custom post types (CPT). That works fine. BUT I would like the query to exclude specific tags from each of the CPTs.

Eg. Include ( (Posts excluding category X) AND all CPTs of post_type 'doc') which have the given search term.

I know how to do this with a new WP_Query object using taxonomy $args... or a plain SQL query but I can't figure out how this would work with $query->set().

Perhaps there is a way to supply a complete taxonomy array... or a raw SQL query?

TIA

add_filter('pre_get_posts','SearchFilter'); function SearchFilter($query) { if( ! is_admin() ) { if ($query->is_search) { $aPT = array(); if ( isset($_GET['include_docs']) ) $aPT[] = 'docs'; if ( isset($_GET['include_general']) ) { $aPT[] = 'post'; $aPT[] = 'page'; } if ( isset($_GET['include_nl']) ) $aPT[] = 'newsletter'; $query->set('post_type', $aPT ); $post_type = get_query_var('post_type'); if($post_type) $post_type = $post_type; else $post_type = $aPT;
        $query->set( 'cat', '-7,-19' )  // should only apply to posts not cpts
    }

} return $query;