我们可能不希望某些文章或页面出现在搜索结果当中,那么我们可以使用 filter(过滤器) 来过滤掉某些文章或页面。根据自己的需要,将下面的代码添加到主题的 functions.php 文件:
排除特定 ID 的文章或页面
//搜索结果排除特定 ID 的文章或页面
function Bing_search_filter_id($query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('post__not_in', array(40,819));//文章或者页面的ID
}
return $query;
}
add_filter('pre_get_posts','Bing_search_filter_id'); |
//搜索结果排除特定 ID 的文章或页面
function Bing_search_filter_id($query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('post__not_in', array(40,819));//文章或者页面的ID
}
return $query;
}
add_filter('pre_get_posts','Bing_search_filter_id');
注意修改第四行的文章或页面 ID
排除某些分类的文章
//搜索结果排除某些分类的文章
function Bing_search_filter_category( $query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('cat','-15,-57'); //分类的ID,前面加负号表示排除;如果直接写ID,则表示只在该ID中搜索
}
return $query;
}
add_filter('pre_get_posts','Bing_search_filter_category'); |
//搜索结果排除某些分类的文章
function Bing_search_filter_category( $query) {
if ( !$query->is_admin && $query->is_search) {
$query->set('cat','-15,-57'); //分类的ID,前面加负号表示排除;如果直接写ID,则表示只在该ID中搜索
}
return $query;
}
add_filter('pre_get_posts','Bing_search_filter_category');
注意修改ID,看注释。
排除所有页面
这个我感觉很实用,建议添加
//搜索结果排除所有页面
function search_filter_page($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','search_filter_page'); |
//搜索结果排除所有页面
function search_filter_page($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','search_filter_page');
来源:
https://www.wpdaxue.com/wordpress-search-filter.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32748.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57