WP User Query with Custom Fields and Search Results

Not sure if you ever found the answer, but just in case, I just did 🙂

There’s a hook I (finally!) found that lets you do this and works in a similar way to posts_where. The hook is pre_user_query. Here’s an example on the Codex: https://developer.wordpress.org/reference/hooks/pre_user_query/#user-contributed-notes

With the above, your function could now look like this:

function user_posts_where( $user_query ) {

    $user_query->query_where = str_replace("meta_key = 'business_information_$", "meta_key LIKE 'business_information_%", $user_query->query_where);

    return $user_query;

}

add_filter('pre_user_query', 'user_posts_where');

Please note that ACF now recommends using the dollar sign as the wildcard, hence business_information_$.

You’d also need to change your meta_query to the below, using business_information_$_business_name instead:

array(
    'key'       => 'business_information_$_business_name',
    'value'     => $usersearch,
    'compare'   => 'LIKE'
)