wordpress - 如何从 Wordpress 搜索结果中排除特定页面
问题描述
当有人在我的网站中搜索时,我不希望包含一些页面。我试图将下面的代码添加到我的 function.php 但它不起作用
add_filter( 'pre_get_posts', 'exclude_pages_search_when_logged_in' );
function exclude_pages_search_when_logged_in($query) {
if ( $query->is_search && is_user_logged_in() )
$query->set( 'post__not_in', array( 6410, 1684, 6385, 278, 6390, 865 ) );
return $query;
}
如何从搜索结果中排除某些页面?
解决方案
add_action
是你用来创建触发器“钩子”的东西——当有事情发生时,做其他事情。
add_filter
用于“挂钩”数据更改/替换。改用下面的代码
add_action( 'pre_get_posts', 'my_search_exclude_filter' );
function my_search_exclude_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 9564, 1213 ) );
}
}
我还描述了其他解决方案是我的博客文章https://naderzad.info/web-development/wordpress-search-result/
推荐阅读
- python - 在 python 中打印图像中对象的边界大小时出现错误
- java - 当出现异常时,回滚完成的操作,但将异常存储在表中
- ios - PageViewController 不工作
- python - Tkinter - 如何在按下按钮时制作弹出框
- javascript - 基于JS中数组中的字符数组拆分字符串
- javascript - 从提及中获取用户 ID
- javascript - Angular 6 GeoFire 初始化 GeoFire 不是构造函数
- c# - 在 PropertyGrid 中带有字符串组合框的 C# 工作流自定义活动
- mongoose - mongo模块导出问题与节点js中的交叉引用文件
- python - 如何使用 python3 将此 input.json 代码转换为 output.json?