0

Hi I am trying to exclude custom post category (specific category id) form the search query. My custom post category taxonomy name is review-cat. I want exclude this custom post taxonomy specific id's.

function wcs_exclude_category_search( $query ) {

    if ( $query->is_search ) {
      $query->set( 'cat', '-33 -46' );
      $query->set( 'post_type', array( 'review' ) );
      
    }
    return $query; 

  
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );
  
Kane
  • 605
  • 2
  • 8
  • 22

1 Answers1

0

I found this. It may help you https://stackoverflow.com/a/52140116/1053190

Or below code copy/paste from https://gist.github.com/billerickson/1332274

/**
 * Exclude term from search query
 * @author Bill Erickson
 * @link http://www.billerickson.net/customize-the-wordpress-query/
 *
 * @param object $query
 */
function be_modify_search_query( $query ) {
    global $wp_the_query;
    if( $query === $wp_the_query && $query->is_search() ) {
        $tax_query = array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'be_modify_search_query' );
Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • thanks for your answer, could you tell me where I add the course ID? – Kane May 27 '21 at 08:54
  • You need to modify the `$tax_query` array based on your requirements. It is just a part of `WP_Query` arguments. Read this for more details https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters – Code Lover May 29 '21 at 07:28