WordPress – Add custom fields in search

Add the code in this article to your functions.php file for a quick result.

function custom_search_query( $query )  {
 	$custom_fields = array(
 		// put all the meta fields you want to search for here
 		"custom_field1",
 		"custom_field2"
 	);
 	$searchterm = $query->query_vars['s'];
 	// we have to remove the "s" parameter from the query, because it will prevent the posts from being found
 	$query->query_vars['s'] = "";
 	if ($searchterm != "")
  	{
 		$meta_query = array('relation' => 'OR');
 		foreach($custom_fields as $cf) {
 			array_push($meta_query, array(
 				'key' => $cf,
 				'value' => $searchterm,
 				'compare' => 'LIKE'
 			));
 		}
 		$query->set("meta_query", $meta_query);
 	};
}
add_filter( "pre_get_posts", "custom_search_query");
add_action( "save_post", "add_title_custom_field");

function add_title_custom_field($postid){
// since we removed the "s" from the search query, we want to create a custom field for every post_title. I don't use post_content, if you also want to index this, you will have to add this also as meta field. 
update_post_meta($postid, "_post_title", $_POST["post_title"]);
}

Source: