自定义分类类别从搜索中排除
Custom taxonomy category exclude from search
您好,我正在尝试从搜索查询中排除自定义 post 类别(特定类别 ID)。我的自定义 post 类别分类名称是 review-cat
。我想排除此自定义 post 分类法特定 ID。
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 );
我找到了这个。可能对你有帮助
或以下来自 https://gist.github.com/billerickson/1332274
的代码 copy/paste
/**
* 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' );
您好,我正在尝试从搜索查询中排除自定义 post 类别(特定类别 ID)。我的自定义 post 类别分类名称是 review-cat
。我想排除此自定义 post 分类法特定 ID。
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 );
我找到了这个。可能对你有帮助
或以下来自 https://gist.github.com/billerickson/1332274
的代码 copy/paste/**
* 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' );