Is it possible to modify a WP search query to return results for private pages when not logged in?

If you want to show Private Posts to non-logged in user then you can try the given code. Also showing Private posts to Non-Logged In User not recommended.

With this approach the private content will expose to all users, which may not be recommended for most cases.

<?php
function include_private_posts_in_search( $query ) {

    if ( $query->is_search && ! is_admin() ) {
        /**
        * Here we have added 'publish' and 'private' post status, we can add more as per the need.
        */
        $query->set( 'post_status', array( 'publish', 'private' ) ); 
    }
    
    return $query;
}

add_filter( 'pre_get_posts', 'include_private_posts_in_search' );

tech