Article directory
Sometimes, we may not want a certain category, article, or page to appear in WordPress site search results.
So we can use filter (filter) to filter out certain articles or web pages.
- This is usingWordPress websiteThe advantages.
WordPress site search excludes specified articles or pages
// WordPress搜索结果排除指定文章或页面ID
function wpsite_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','wpsite_search_filter_id');- Please note:The ID of the article or page on line 4 needs to be modified.
WordPress site search excludes certain categories of articles
// WordPress搜索结果排除某分类的文章
function wpsite_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','wpsite_search_filter_category');- Please note: Modify the ID and view the code comments.
WordPress site search excludes all pages
This is very practical, it is recommended to add ▼
// WordPress搜索结果排除所有页面
function search_filter_page($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','search_filter_page');The Search Exclude plugin does not allow an article page to appear in the site's search results
- we useWordPress website, originally does not have the function of excluding a certain article page in the site search.
- However, by adding WordPress code, or installingWordPress pluginto achieve this function.
- The Search Exclude plugin allows you to exclude specific articles from your site's search results at any time.
After installing the Search Exclude plugin, you will see new features on the right side of the article editing interface ▼

Here's how to install a WordPress plugin ▼
WordPress site search does not display the specified article
As long as this "Exclude from Search Results" option is checked, the article will no longer appear in the site's on-site search results.
When you search in the WordPress front-end site, you will find that you cannot find excluded articles▼

In the management interface of the Search Exclude plugin, you can view all articles or pages that have been excluded from search on the site▼

- And, they can be removed in bulk.
Hopefully, the article "How to exclude specific category/post page from site search results in WordPress?" shared on Chen Weiliang's blog ( https://www.chenweiliang.com/ ) will be helpful to you.
Feel free to share this article's link: https://www.chenweiliang.com/cwl-1057.html
