It would appear that I can hook into the get_search_query filter to restore the result of get_search_query() to what it was before I removed ‘s’ from query_vars. This operates the way I want it to on the particular site that I am testing it on, but I am not sure if this s property is used in other ways that I have not considered, so even though this works in the current circumstance, I’d be glad to hear of alternatives that don’t involve unsetting ‘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);
}
});