Filter Search Query by Custom Taxonomy

functions.php

/*////////////////////////////////////////////////
 //
 //	Filter search query by custom taxonomy
 //
 ////////////////////////////////////////////////*/

function filtered_search($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
        
        if (isset($_GET['gcats']) && is_array($_GET['gcats'])) {

            $tax_query = array(
                array(
                    'taxonomy'  => 'guide_category',
                    'field'     => 'term_id',
                    'terms'     => $_GET['gcats'],
                )
            );

            $query->set('tax_query', $tax_query);
        }

        if (isset($_GET['groles']) && is_array($_GET['groles'])) {

            $requested_roles = $_GET['groles'];

            $roles_query = array();            
            foreach($requested_roles as $requested_role) {
                if($_GET['s']) {
                    $role_query = array(
                        'key' => $requested_role,
                        'value' => $_GET['s'],
                        'compare' => 'LIKE',
                    );
                } else {
                    $role_query = array(
                        'key' => $requested_role,
                        'value' => array(''),
                        'compare' => 'NOT IN',
                    );
                }
                $roles_query['relation'] = 'OR';
                array_push($roles_query, $role_query);
            }

            //print_r($roles_query);
            
            $query->set('meta_query', $roles_query);
        }
        
        
    }
  }
}

add_action('pre_get_posts','filtered_search');

searchform.php


<?php $unique_id = esc_attr( pmo_unique_id( 'search-form-' ) ); ?>

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">

        <br>
    <label for="<?php echo $unique_id; ?>">
            <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'pmo' ); ?></span>
    </label>
    <input type="search" id="<?php echo $unique_id; ?>" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'pmo' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
    <button type="submit" class="search-submit"><?php echo pmo_get_svg( array( 'icon' => 'search' ) ); ?><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'pmo' ); ?></span></button>
    <br><br>
    <div class="flex-display">
        <div class="flex-1of4">
            <label>Filter by category:</label>
            <br>
            <?php if( $terms = get_terms( array('taxonomy' => 'guide_category', 'hide_empty' => false, 'exclude' => array(10), )) ) : //Exclude FAQ guide_category (id = 10)
                echo '<select multiple="multiple" name="gcats[]" class="form-control">';
                foreach ( $terms as $term ) :
                        echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
                endforeach;
                echo '</select>';
            endif; ?>
        </div>
        <div class="flex-1of4">
            <label>Filter by role:</label>
            <?php $groups = acf_get_field_groups(array('post_type' => 'guide'));
            if( $roles = acf_get_fields($groups[1][key])) :
                echo '<select multiple="multiple" name="groles[]" class="form-control">';
                foreach ( $roles as $role ) :
                        echo '<option value="' . $role['name'] . '">' . $role['label'] . '</option>'; // Name of the custom field as the value of an option
                endforeach;
                echo '</select>';
            endif; ?>
        </div>
    </div>    
    
</form>