admin管理员组文章数量:1333386
How do I exclude a specific page (and child pages) from the search results?
I've seen posts showing how to exclude all pages. I don't want that. I want to target a page and it's children and exclude them and only them.
How do I exclude a specific page (and child pages) from the search results?
I've seen posts showing how to exclude all pages. I don't want that. I want to target a page and it's children and exclude them and only them.
Share Improve this question asked Jun 23, 2020 at 14:35 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges1 Answer
Reset to default 1// Filter to exclude a page and it's child pages from search query
function myfilter_exclude_pages_from_search( $query ) {
// Save the post id of the page to be excluded in $parent_post_id_to_exclude
$parent_post_id_to_exclude = 111; // Replace 111 with actual page id
// Convert the post id to an array
$parent_post_id_arr = array( $parent_post_id_to_exclude );
// Apply filter only if this is a search query and not on admin panel
if ( $query->is_search && !is_admin() ) {
// Search only for pages
$query->set( 'post_type', 'page');
// Exclude the page id
$query->set( 'post__not_in', $parent_post_id_arr );
// Exclude all child pages of selected page id
$query->set( 'post_parent__not_in', $parent_post_id_arr );
}
return $query;
}
add_filter( 'pre_get_posts', 'myfilter_exclude_pages_from_search' );
Read below article for other use cases for pre_get_posts
https://www.wpbeginner/plugins/how-to-exclude-specific-pages-authors-and-more-from-wordpress-search/
Reference for parameters to WP_Query : https://developer.wordpress/reference/classes/wp_query/
本文标签: How do I exclude a specific page (and child pages) from the search results
版权声明:本文标题:How do I exclude a specific page (and child pages) from the search results? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742324421a2453438.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论