朱纯树博客
VPS测评推荐网站
cloudacead cloudacead

WordPress搜索结果排除指定页面/文章/自定义分类

今天在搜索自己站点文章的时候,发现收录的常用网址居然也在搜索结果页面显示。

这就很尴尬了,搜索结果也太广泛了。

用户搜索到一些无关的内容,估计也很不爽,然后就尝试在搜索结果页面排除一些内容,例如:指定页面、指定文章、指定自定义分类(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搜索结果排除指定页面/文章/自定义分类

赞(0) 打赏
未经允许不得转载:VPS测评推荐网站 - 朱纯树博客 » WordPress搜索结果排除指定页面/文章/自定义分类

评论 145

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #150

    Long-Term Effects. Get here.
    can i purchase cheap nexium prices
    п»їMedicament prescribing information. Read information now.

    WillieErert8分钟前回复
  2. #149

    I think most think, if anything, it doesn t hurt buy clomiphene citrate

    Chiellare15小时前回复
  3. #148

    Everything information about medication. drug information and news for professionals and consumers.
    ivermectin cream
    Read now. Everything about medicine.

    Jesselap23小时前回复
  4. #147

    Read information now. Get here.
    ivermectin 3mg pill
    Comprehensive side effect and adverse reaction information. Read information now.

    KevinCaf1天前回复
  5. #146

    Drug information. Generic Name.
    stromectol 3 mg price
    earch our drug database. Read information now.

    Ronaldhax2天前回复
  6. #145

    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.

    KevinCaf2天前回复
  7. #144

    Comprehensive side effect and adverse reaction information. Learn about the side effects, dosages, and interactions.
    ivermectin brand name
    Read here. Get here.

    KevinCaf3天前回复
  8. #143

    Best and news about drug. drug information and news for professionals and consumers.
    https://stromectolst.com/# ivermectin for humans
    Everything about medicine. Read now.

    Ronaldhax3天前回复
  9. #142

    Some trends of drugs. Actual trends of drug.
    https://stromectolst.com/# ivermectin usa price
    Get here. Generic Name.

    Jesselap3天前回复
  10. #141

    Commonly Used Drugs Charts. Medscape Drugs & Diseases.
    ivermectin 18mg
    Some trends of drugs. Medscape Drugs & Diseases.

    KevinCaf3天前回复