WordPress搜索过滤器仅适用于带有孩子的孩子的孩子的页面
1 个回答
- 投票数
您可能可以执行以下操作.它的效率可能较低,但是应该找到给定页面的所有后代,而不仅仅是直接子代.
function find_descendants($post_id) {
$descendant_ids = array();
$pages = get_pages("child_of=$post_id");
foreach ($pages as $page) { array_push($descendant_ids, $page->ID); }
return $descendant_ids;
}
function SearchFilter($query) {
if ($query->is_search) {
$query->set ( 'post__in', find_descendants(21) );
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
You can probably do something like this. It's probably less efficient, but should find all descendants of a given page rather than just direct children.
function find_descendants($post_id) {
$descendant_ids = array();
$pages = get_pages("child_of=$post_id");
foreach ($pages as $page) { array_push($descendant_ids, $page->ID); }
return $descendant_ids;
}
function SearchFilter($query) {
if ($query->is_search) {
$query->set ( 'post__in', find_descendants(21) );
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
浏览该网站的问题后,我找到了解决方法:
当我这样做时,其唯一的搜索对象是父母21的孩子,而不是父母21的孩子.
此外,query_vars仅接受post_parent的单个值.
对此的任何解决方案都将受到高度赞赏.
谢谢