Filter - Search : how to expand to search to a specific custom field?

Hi everyone,
I’m working on expanding the Search - Filter to another custom field (ACF) called card_transmutable. I use s as URL parameter, and my Query Loop is placed in an Archive Template.
So I got the filter to fire, but only on refresh. On AJAX search, or even if I use “submit”, it doesn’t filter as expected. Of course, the URL stays the same after the AJAX search and the refresh, but it looks like the refresh triggers native Wordpress search, not the bricks specific Query.
Here is the code I’m using, based on Adam Balee’s code (which includes all meta fields instead of just the one I need) :

<?php
/**
 * Make every query that has ?s=… search title + the ACF field card_transmutable.
 */

add_action( 'init', function() {

    // JOIN
    add_filter( 'posts_join', function( $join ) {
        global $wpdb;
        if ( isset($_GET['s']) && $_GET['s'] !== '' ) {
            $join .= ' LEFT JOIN ' . $wpdb->postmeta .
                     ' ON ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
        }
        return $join;
    } );

    // WHERE
    add_filter( 'posts_where', function( $where ) {
        global $wpdb;
        if ( isset($_GET['s']) && $_GET['s'] !== '' ) {
            $where = preg_replace(
                "/\(\s*{$wpdb->posts}\.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(".$wpdb->posts.".post_title LIKE $1)
                 OR ((".$wpdb->postmeta.".meta_key = 'card_transmutable')
                 AND ".$wpdb->postmeta.".meta_value LIKE $1)",
                $where
            );
        }
        return $where;
    } );

    // DISTINCT
    add_filter( 'posts_distinct', function( $distinct ) {
        if ( isset($_GET['s']) && $_GET['s'] !== '' ) {
            return "DISTINCT";
        }
        return $distinct;
    } );

});

Any idea on how to resolve this?

Wow, Claude got it! Here is the fixed code, working on AJAX Search:

// Add filters once, globally
add_filter( 'posts_join', function( $join, $query ) {
    global $wpdb;
    
    // Only apply to carte queries with search term
    $post_type = $query->get('post_type');
    $search_term = $query->get('s');
    
    $is_carte = (is_array($post_type) && in_array('carte', $post_type)) || $post_type === 'carte';
    
    if ( ! $is_carte || ! $search_term || $search_term === '' ) {
        return $join;
    }
    
    // Prevent duplicate joins
    if ( strpos($join, 'pm_card_search') === false ) {
        $join .= " LEFT JOIN {$wpdb->postmeta} AS pm_card_search 
                  ON {$wpdb->posts}.ID = pm_card_search.post_id 
                  AND pm_card_search.meta_key = 'card_transmutable'";
        error_log('JOIN applied for: ' . $search_term);
    }
    
    return $join;
}, 10, 2 );

add_filter( 'posts_where', function( $where, $query ) {
    global $wpdb;
    
    // Only apply to carte queries with search term
    $post_type = $query->get('post_type');
    $search_term = $query->get('s');
    
    $is_carte = (is_array($post_type) && in_array('carte', $post_type)) || $post_type === 'carte';
    
    if ( ! $is_carte || ! $search_term || $search_term === '' ) {
        return $where;
    }
    
    // Remove WordPress default search clause
    $where = preg_replace('/AND \(\(\(.+?post_title.+?\)\)\)/', '', $where);
    
    // Add our custom search
    $where .= $wpdb->prepare(
        " AND (pm_card_search.meta_value LIKE %s OR {$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_content LIKE %s)",
        '%' . $wpdb->esc_like($search_term) . '%',
        '%' . $wpdb->esc_like($search_term) . '%',
        '%' . $wpdb->esc_like($search_term) . '%'
    );
    
    error_log('WHERE applied for: ' . $search_term);
    return $where;
}, 10, 2 );

add_filter( 'posts_distinct', function( $distinct, $query ) {
    // Only apply to carte queries with search term
    $post_type = $query->get('post_type');
    $search_term = $query->get('s');
    
    $is_carte = (is_array($post_type) && in_array('carte', $post_type)) || $post_type === 'carte';
    
    if ( ! $is_carte || ! $search_term || $search_term === '' ) {
        return $distinct;
    }
    
    error_log('DISTINCT applied for: ' . $search_term);
    return 'DISTINCT';
}, 10, 2 );