看起来我可以挂接到get\\u search\\u查询过滤器中,将get\\u search\\u query()的结果恢复到从query\\u vars中删除“s”之前的状态。在我测试它的特定站点上,它的运行方式是我想要的,但我不确定它的属性是否以我没有考虑过的其他方式使用,因此,即使在目前的情况下,这是可行的,我很高兴听到不涉及不稳定“s”的替代方案。
add_action(\'pre_get_posts\', function (\\WP_Query $query) {
if (!$query->is_search()) {
return;
}
if (is_admin()) {
return;
}
$q = get_search_query();
if (!$q) {
return;
}
$search_parameters = array("q" => $q);
$search_parameters = apply_filters(\'custom_search_parameters\', $search_parameters);
$search_results = custom_search($search_parameters);
if ($search_results) {
unset($query->query_vars[\'s\']);
add_filter(\'get_search_query\', function ($search) use ($q) {
$search = $q;
$search = urldecode($search);
$search = strip_tags($search);
$search = filter_var($search, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
return $search;
});
$query->query_vars[\'post__in\'] = $search_results[\'post_ids\'];
$query->query_vars[\'orderby\'] = \'post__in\';
$query = apply_filters(\'custom_search_query\', $query);
}
});