在WordPress搜索结果中排除自定义文章类型

知更鸟 WordPress评论416阅读模式

如不想让 WordPress 自定义文章类型,显示在搜索结果中,可以使用下面的代码。

文章源自知更鸟-https://zmingcx.com/exclude-custom-post-type-from-search.html

在WordPress搜索结果中排除自定义文章类型

搜索结果中排除自定义文章类型

文章源自知更鸟-https://zmingcx.com/exclude-custom-post-type-from-search.html

将代码添加到当前主题函数模板 functions.php 中:文章源自知更鸟-https://zmingcx.com/exclude-custom-post-type-from-search.html

function zm_filter_search( $query ) {
	if ( ! $query->is_admin && $query->is_search ) {
		// 添加想要显示在搜索结果中的文章类型
		$query->set( 'post_type', array( 'post', 'page', 'book' ) );
	}
	return $query;
}
add_filter( 'pre_get_posts', 'zm_filter_search' );

其中book是自定义文章类型,如果不想在搜索结果中显示删除即可,同样不想让页面显示在搜索结果中可以删除page。文章源自知更鸟-https://zmingcx.com/exclude-custom-post-type-from-search.html 文章源自知更鸟-https://zmingcx.com/exclude-custom-post-type-from-search.html

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
评论  0  访客  0
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定