If you have several hundred or thousand posts that must be searched through, changing the ACF post object to only search in titles is a huge performance enhancement.
In functions.php:
function add_search_only_titles_filter($args, $field, $post_id) {
add_filter('posts_search', '__search_by_title_only', 500, 2);
return $args;
}
function __search_by_title_only( $search, &$wp_query ) {
global $wpdb;
if ( empty( $search ) )
return $search; // skip processing - no search term in query
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search =
$searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter('acf/fields/post_object/query', 'add_search_only_titles_filter', 10, 3);