我有一些页面,我不希望它被包括在任何人在我的网站内搜索。我尝试将下面的代码添加到我的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;
}如何从搜索结果中排除某些页面?
发布于 2021-02-03 15:02:41
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/中描述了其他解决方案
https://stackoverflow.com/questions/63088728
复制相似问题