简体   繁体   中英

Custom taxonomy category exclude from search

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 );
  

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' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM