I am trying to assign relate posts using the ACF relationship field. It has typeahead search to search by post titles.

I would like to add meta_data search to the ACF relationship search.

enter image description here

This question got my query working properly, but the search field still says there are no results.

enter image description here

I think that the typeahead is removing results because the typeahead only matches the title.

Any ideas how to edit this to allow meta data to return in the search results?

  • Looks like the $wpdb query is only looking for titles. You would need to change that SQL statement to look for taxonomies as well. Look at the SQL statement inside $wpdb->prepare()
    – gdaniel
    Feb 18 '15 at 20:26
  • Actually the sql is searching both. I am using this code wordpress.stackexchange.com/questions/178484/…
    – Bryan
    Feb 18 '15 at 23:02
  • I think it is on the front end with JS that my results are removed.
    – Bryan
    Feb 18 '15 at 23:02
  • Keep in mind this is an inherently expensive query to make, and will be very slow, don't do this kind of query on the frontend
    – Tom J Nowell
    Sep 1 '16 at 15:25

First, read this post to understand my answer Search that will look in custom field, post title and post content

You may want to use the acf/fields/relationship/query/ however, when adding the args:

$args['meta_query'] = array(array(
     'key' => 'your_meta',
     'value' => $args['s'],
     'compare' => 'LIKE',

you will find that using that query, Wordpress will search the posts that contains your search string in the title AND in the meta field.

So you have to add a couple of filters to make this works:

function search_custom_meta_acf_add_join($joins) {
    global $wpdb;
    return $joins . " INNER JOIN {$wpdb->postmeta} as CMS15 ON ({$wpdb->posts}.ID = CMS15.post_id)";

function search_custom_meta_acf_alter_search($search,$qry) {
    global $wpdb;
    $add = $wpdb->prepare("(CMS15.meta_key = 'your_field_name' AND CAST(CMS15.meta_value AS CHAR) LIKE '%%%s%%')",$qry->get('s'));
    $pat = '|\(\((.+)\)\)|';
    $search = preg_replace($pat,'(($1 OR '.$add.'))',$search);
    return $search;
function modify_acf_relationship_search_query ($args, $field, $post ) {
    return $args;
add_filter('acf/fields/relationship/query/name=your_field_name', 'modify_acf_relationship_search_query', 10, 3);

What this code does is, basically, to modify the Query that searches the posts since ACF uses the wordpress built-in search functionality for searching in the relationship field. You don't have to worry about the filters that modifies the search query because they remove themselves when used.

According to the answer I cited above, he doesn't use a custom table name in the inner join but I did so it doesn't cause any trouble if you still want to use the $args['meta_query'] parameter for a more refined filtering.

Not the answer you're looking for? Browse other questions tagged or ask your own question.