今天在搜索自己站点文章的时候,发现收录的常用网址居然也在搜索结果页面显示。
这就很尴尬了,搜索结果也太广泛了。
用户搜索到一些无关的内容,估计也很不爽,然后就尝试在搜索结果页面排除一些内容,例如:指定页面、指定文章、指定自定义分类(post_type)等内容。
嗯,折腾一阵子也算是折腾好了,现在搜索页只会显示搜索相关的文章了。
方法也挺简单,几行代码就能搞定,下面分享几段搜索结果排除内容的wordpress代码
搜索结果排除指定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');
搜索结果排除指定分类的文章
//搜索结果排除某些分类的文章
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 search_filter_page($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','search_filter_page');
搜索结果排除自定义分类(post_type)
//搜索结果排除自定义分类(post_type)
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'site' )); }
return !$query;
}
add_filter( 'the_search_query', 'searchAll' );
文章来源于互联网:wordpress搜索结果排除指定页面/文章/自定义分类
Long-Term Effects. Get here.
can i purchase cheap nexium prices
п»їMedicament prescribing information. Read information now.
I think most think, if anything, it doesn t hurt buy clomiphene citrate
Everything information about medication. drug information and news for professionals and consumers.
ivermectin cream
Read now. Everything about medicine.
Read information now. Get here.
ivermectin 3mg pill
Comprehensive side effect and adverse reaction information. Read information now.
Drug information. Generic Name.
stromectol 3 mg price
earch our drug database. Read information now.
Comprehensive side effect and adverse reaction information. What side effects can this medication cause?
stromectol 12mg
Everything information about medication. drug information and news for professionals and consumers.
Comprehensive side effect and adverse reaction information. Learn about the side effects, dosages, and interactions.
ivermectin brand name
Read here. Get here.
Best and news about drug. drug information and news for professionals and consumers.
https://stromectolst.com/# ivermectin for humans
Everything about medicine. Read now.
Some trends of drugs. Actual trends of drug.
https://stromectolst.com/# ivermectin usa price
Get here. Generic Name.
Commonly Used Drugs Charts. Medscape Drugs & Diseases.
ivermectin 18mg
Some trends of drugs. Medscape Drugs & Diseases.