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?

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');
               $post_type = $post_type;
               $post_type = $aPT;

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

1 Answer 1


You can set tax query in pre_get_posts hook as well:

$query->set('tax_query', array(__standart parameters__));
  • Didn't know that. Will try!
    – jchwebdev
    Commented Jan 24, 2015 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.