wordpress页面_如何从WordPress搜索中排除特定的页面,作者等

wordpress页面

Do you want to exclude specific pages, authors, and more from WordPress search? By default, WordPress search includes all posts and pages in the search results. In this article, we will show you how to easily exclude specific pages, posts, authors, categories, and more from WordPress search results.

您要从WordPress搜索中排除特定页面,作者等吗? 默认情况下,WordPress搜索包括搜索结果中的所有帖子和页面。 在本文中,我们将向您展示如何轻松地从WordPress搜索结果中排除特定页面,帖子,作者,类别等。

Exclude pages, authors, category, tag, and more from WordPress search
为什么要从WordPress搜索中排除项目? (Why Exclude Items from WordPress Search?)

The default WordPress search feature shows results from all WordPress posts, pages, and custom post types. This is acceptable for most websites and does not affect WordPress SEO or performance.

默认的WordPress搜索功能显示所有WordPress帖子,页面和自定义帖子类型的结果。 这对于大多数网站都是可以接受的,并且不会影响WordPress SEO或性能。

However if you are running an online store, then there are some pages that you may not want to appear in search results. For example, the checkout page, my account page, or a thank you page after successful downloads.

但是,如果您正在运行在线商店 ,那么有些页面可能不希望出现在搜索结果中。 例如,成功下载后的结帐页面,我的帐户页面或感谢页面。

Similarly, if you are running a WordPress membership website, or a LMS plugin, then there would be pages and custom post types on your website that you may want to exclude from search results.

同样,如果您正在运行WordPress成员资格网站LMS插件 ,则您的网站上可能会有一些页面和自定义帖子类型 ,您可能希望将它们从搜索结果中排除。

Some website owners may want to hide a category or taxonomy, while others may want to hide posts from specific authors. Optimizing your site-search by excluding unnecessary items offers a better user experience and improves your website’s usability.

一些网站所有者可能希望隐藏类别或分类法 ,而另一些网站所有者可能希望隐藏特定作者的帖子。 通过排除不必要的项目来优化网站搜索,可以提供更好的用户体验并提高网站的可用性。

That being said, let’s take a look at how to easily exclude items from WordPress search.

话虽如此,让我们看一下如何轻松地从WordPress搜索中排除项目。

1.从搜索中排除特定的帖子,页面和自定义帖子类型 (1. Exclude Specific Posts, Pages, and Custom Post Types from Search)

The first thing you need to do is install and activate the Search Exclude plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活Search Exclude插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, edit the post, page, or custom post type that you want to exclude from the search result. On the edit screen, you will see a search exclude box.

激活后,编辑要从搜索结果中排除的帖子,页面或自定义帖子类型。 在编辑屏幕上,您会看到一个搜索排除框。

Exclude from search box

Simply check ‘Exclude from Search Results’ checkbox and don’t forget to save your post/page. This particular post/page will not appear in WordPress search results anymore.

只需选中“从搜索结果中排除”复选框,别忘了保存您的帖子/页面。 这个特定的帖子/页面将不再出现在WordPress搜索结果中。

To view all the items that you have excluded from search, go to Settings » Search Exclude page. Here you will see a list of items you have excluded from WordPress search results.

要查看已从搜索中排除的所有项目,请转到“设置”»“搜索排除”页面。 在这里,您将看到从WordPress搜索结果中排除的项目列表。

Content you have excluded from WordPress search

If you want to remove the restriction, simply uncheck the box next to the item you want to add back and click on the save changes button.

如果要删除限制,只需取消选中要添加的项目旁边的框,然后单击“保存更改”按钮。

2.从WordPress搜索中排除特定类别,标签,自定义分类法 (2. Exclude Specific Category, Tag, Custom Taxonomy From WordPress Search)

This method requires you to add code to your WordPress website. If you haven’t done this before, then check out our guide on how to copy and paste code snippets in WordPress.

此方法要求您将代码添加到WordPress网站。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码段

First, you need to find the category ID that you want to exclude.

首先,您需要找到要排除的类别ID

Next, you need to add the following code to your theme’s functions.php file or a site-specific plugin.

接下来,您需要将以下代码添加到主题的functions.php文件或特定站点的插件中



function wpb_search_filter( $query ) {
	if ( $query->is_search && !is_admin() )
		$query->set( 'cat','-7' );
	return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

Don’t forget to replace 7 with the ID of category you want to exclude.

不要忘记将7替换为您要排除的类别ID。

Now, let’s suppose you want to exclude more than one category. This is how you will modify the code to exclude multiple categories.

现在,假设您要排除多个类别。 这是您将如何修改代码以排除多个类别。


function wpb_search_filter( $query ) {
	if ( $query->is_search && !is_admin() )
		$query->set( 'cat','-7, -10, -21' );
	return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

We have simply added the category IDs that we want to exclude separated by commas.

我们仅添加了要排除的类别ID,以逗号分隔。

Exclude Specific Tags from WordPress Search

从WordPress搜索中排除特定标签

If you want to exclude posts filed under specific tag, then you can use the following code.

如果要排除在特定标签下归档的帖子,则可以使用以下代码。


function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
		$query->set( 'tag','-19' );
	return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

Don’t forget to replace 19 with the ID of tag you want to exclude.

不要忘记将19替换为您要排除的标签ID。

Similarly, you can modify the code to exclude multiple tags as well.

同样,您可以修改代码以排除多个标签。


function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
		$query->set( 'tag','-19, -27, -56' );
	return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

Excluding Specific Terms in a Custom Taxonomy From WordPress Search

从WordPress搜索中排除自定义分类法中的特定术语

If you want to exclude a term in a custom taxonomy from WordPress search results, then you will need to add the following code.

如果要从WordPress搜索结果中排除自定义分类法中的术语,则需要添加以下代码。



function wpb_modify_search_query( $query ) {
	global $wp_the_query;
	if( $query === $wp_the_query && $query->is_search() ) {
		$tax_query = array(
			array(
				'taxonomy' => 'genre',
				'field' => 'slug',
				'terms' => 'action',
				'operator' => 'NOT IN',
			)
		);
		$query->set( 'tax_query', $tax_query );
	}
}
add_action( 'pre_get_posts', 'wpb_modify_search_query' );


Don’t forget to replace ‘genre’ with the custom taxonomy and ‘action’ with the term you want to exclude.

不要忘记用自定义分类法替换“体裁”,并用您要排除的术语替换“动作”。

3.从WordPress搜索中排除特定作者 (3. Exclude Specific Author From WordPress Search)

If you want to exclude posts created by a specific author from WordPress search result, then there are two ways to do that.

如果要从WordPress搜索结果中排除特定作者创建的帖子,则有两种方法可以做到这一点。

If the author has just a few posts, and you are sure they will not be adding any more posts, then you can just use the first method in this article to exclude their posts from WordPress search.

如果作者只有几篇文章,并且您确定他们不会再添加更多文章,那么您可以使用本文中的第一种方法从WordPress搜索中排除他们的文章。

However if there are a lot of posts written by an author, then you can use the following code to exclude all of them from WordPress search results.

但是,如果有很多作者撰写的帖子,那么您可以使用以下代码从WordPress搜索结果中排除所有帖子。


function wpb_search_filter( $query ) {
	if ( $query->is_search && !is_admin() )
		$query->set( 'author','-24' );
	return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

Don’t forget to replace 24 with the user ID of the author you want to exclude.

不要忘记用您要排除的作者的用户ID替换24。

You can also use the same code to exclude multiple authors by adding their user IDs separated by comma.

您还可以使用相同的代码通过添加其作者ID(以逗号分隔)来排除多个作者。


function wpb_search_filter( $query ) {
	if ( $query->is_search && !is_admin() )
		$query->set( 'author','-24, -12, -19' );
	return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

We hope this article helped you learn how to explude specific pages, authors, and more from WordPress search. You may also want to see our list of the 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视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/plugins/how-to-exclude-specific-pages-authors-and-more-from-wordpress-search/

wordpress页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值