To avoid modifying the main query, you might consider adding a custom query variable with PHP:
add_filter( 'query_vars', function( $vars ) {
$vars[] = 'qls'; // As query-loop-search.
return $vars;
} );
and use ?qls=foo
instead of ?s=foo
.
To target the corresponding query loop, add the text :query-loop-search
into the search box of the query loop block in the editor.
Then we could grab the value of the custom query variable ?qls=foo
and set it as the search variable for the target query with a little more PHP:
add_action( 'pre_get_posts', function( \WP_Query $q ) {
$qls = $q->get( 'qls' );
if ( empty( $qls ) || is_admin() || $q->is_main_query() ) {
return;
}
if ( $q->is_search() && ':query-loop-search' === trim( $q->get( 's' ) ) ) {
$q->set( 's', $qls );
}
} );
This should only apply for sub-queries (from query-loop blocks) on the front-end.
Note that this is untested.
Another approach could be to use Javascript and the built-in REST API to fetch data from a custom search input field.