A possible solution is to use the pre_get_posts
action:
add_action('pre_get_posts', 'custom_search_query');
function custom_search_query($query) {
// run only if is a search query and if the search string is not empty
if($query->is_search() && $query->is_main_query() && get_query_var('s', false)) {
// here you can use your custom query having 'get_query_var('s', false)' as $Search string
//"SELECT SQL_CALC_FOUND_ROWS *..........
// just modify your query to return only the IDS of the posts found and put them in an array $ids
$ids=[1,2,3,4]; // example of IDS found by your custom query
// unset the query_var otherwise the custom SQL will not work
unset( $query->query_vars['s'] );
// set the IDS as 'post__in' in the $query
$query->set( 'post__in', $ids );
}
}
Having removed the $query->query_vars['s']
the only missing thing will be the searched string in the template file ‘search.php’ where, normally, there will be something like: ( from twentytwentyone theme)
<h1 class="page-title">
<?php
printf(
/* translators: %s: Search term. */
esc_html__( 'Results for "%s"', 'twentytwentyone' ),
'<span class="page-description search-term">' . esc_html( get_search_query() ) . '</span>'
);
?>
</h1>
So get_search_query()
will print an empty string instead of the searched string, just replace the get_search_query()
function with $_GET['s']