Preview:
<?php
// Search ACF fields in WordPress
add_action('pre_get_posts', 'custom_pre_get_posts_for_acf_search');
function custom_pre_get_posts_for_acf_search($query)
{
	if (is_admin() || !$query->is_main_query() || !$query->is_search()) {
		return;
	}

	if (isset($_GET['s_q_fulltext']) && !empty($_GET['s_q_fulltext'])) {
		$query->set('s', sanitize_text_field($_GET['s_q_fulltext']));
	}
}


function custom_search_acf_fields($where, $query)
{
	if (is_admin() || !$query->is_main_query() || !$query->is_search()) {
		return $where;
	}

	global $wpdb;

	$search_term = $query->get('s');
	if (empty($search_term))
		return $where;

	$like = '%' . $wpdb->esc_like($search_term) . '%';

	$where .= $wpdb->prepare("
		OR EXISTS (
			SELECT 1 FROM $wpdb->postmeta
			WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID
			AND $wpdb->postmeta.meta_value LIKE %s
		)
	", $like);

	return $where;
}
add_filter('posts_where', 'custom_search_acf_fields', 10, 2);
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter