Support

Account

Home Forums General Issues Search Relational Items by their ACF Fields Reply To: Search Relational Items by their ACF Fields

  • I just realized that what I did will cause to only search the field when that’s not really what we want. It would need to be more like this

    
    add_filter('acf/fields/relationship/query/name=YOUR_FIELD_NAME', 'add_artist_meta_query', 10, 3);
    function add_artist_meta_query($args, $field, $post_id) {
      if (isset($args['s'])) {
        $args['meta_query] = array(
          'relation' => 'OR'
          array(
            'key' => 'YOUR_FIELD_NAME',
            'value' => $args['s'],
            'compare' => 'LIKE'
          )
          array(
            'key' => 'YOUR_FIELD_NAME',
            'value' => $args['s'],
            'compare' => 'NOT LIKE'
          )
        );
      }
      return $args;
    }
    

    I think that’s the only way to accomplish post searching the title and the meta field and returning results for each. I may also be missing what’s needed for searching the title. You may need to alter that as well, but I can’t be sure. Probably a lot more complicated that I originally thought and would take quite a bit of testing to get it right.