![](https://i-blog.csdnimg.cn/blog_migrate/5b0f0d2220a808bffdafe60787a0756e.png)
wordpress页面
By default, WordPress Search feature displays published posts and published pages in search results. Often when users are looking for something in a blog, it is most likely a post rather than a page. In this article, we will show you how to make your search more relevant and less crowded by excluding pages from WordPress search results.
默认情况下,WordPress搜索功能在搜索结果中显示已发布的帖子和已发布的页面。 通常,当用户在博客中查找内容时,它很可能是帖子而不是页面。 在本文中,我们将向您展示如何通过从WordPress搜索结果中排除页面来使搜索更相关,更不拥挤。
![Exclude pages frm WordPress search results Exclude pages frm WordPress search results](https://i-blog.csdnimg.cn/blog_migrate/6e74b5c35b4291ce4ddca54825eea893.png)
First, you add the following code to your theme’s functions.php file or a site-specific plugin.
首先,将以下代码添加到主题的functions.php文件或特定于站点的插件中 。
//Exclude pages from WordPress Search
if (!is_admin()) {
function wpb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}
Explanation: This code first checks that the search is not originating from the WordPress admin pages. If it is not, then it searches for posts by setting the post_type parameter.
说明:此代码首先检查搜索是否源自WordPress管理页面。 如果不是,那么它将通过设置post_type参数来搜索帖子。
You can also make it do the opposite by setting the post_type to pages, so it only return pages in the search results.
您还可以通过将post_type设置为pages来使其相反,因此它仅返回搜索结果中的页面。
We hope this article helped you learn how to exclude pages from WordPress search results. You may also want to see our list of best WordPress search plugins to improve your site search.
我们希望本文能帮助您学习如何从WordPress搜索结果中排除页面。 您可能还想查看我们的最佳WordPress搜索插件列表,以改善您的站点搜索。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-exclude-pages-from-wordpress-search-results/
wordpress页面