The WordPress default search has never been the most powerful tool in the WordPress arsenal, and hasn’t changed much over the years. Developer and focus group discussions have promised much, but it’s still the same old basic search functionality that existed way back when.

However, it’s possible to extend the search to include any custom fields you may have set up with the addition of a few lines of code. Add the following code to your functions.php file in your theme or child theme folder, and you should be able to search both the front end and admin panel via your custom fields, with the minimum of fuss.

function custom_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }
    
    return $join;
}
add_filter('posts_join', 'custom_search_join' );

function custom_search_where( $where ) {
    global $wpdb;
   
    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'custom_search_where' );

function custom_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'custom_search_distinct' );
[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]