Support

Account

Home Forums General Issues Search Relational Items by their ACF Fields

Solving

Search Relational Items by their ACF Fields

  • I love the relational field, but in my specific situation, I’m having users search through the “music” custom post type. Within the custom post type, the title is the song title, and there is an ACF field for “Artist”.

    Is there a way to filter the AJAX search results so that when typing in a “Relational” field, it not only searches for CPT titles, but also the fields within those CPT’s?

    I’ve already modified the output so that it shows like “{Title} – {Artist}”, but I’d like if people could search by artist as well.

  • I realize that what I’m asking is a version of this question:
    http://support.advancedcustomfields.com/forums/topic/wp_query-search-results-with-s-and-meta_query-at-the-same-time/

    But the solution is to modify the query. I see that I can modify the $args for the query, but not the query itself. Is there a way to modify the query that is performed to do the AJAX search for relational objects?

  • That is basically what you need to do, alter the query used to get the posts. I’m not exactly sure this is 100% correct because I have not taken the time to test it and I don’t know what kind of field you are searching, but it should have the basics.

    
    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(
          array(
            'key' => 'YOUR_FIELD_NAME',
            'value' => $args['s'],
            'compare' => 'LIKE'
          )
        );
      }
      return $args;
    }
    
    $args = apply_filters('acf/fields/relationship/query/name=' . $field['name'], $args, $field, $options['post_id'] );
    
  • 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.

  • The answer linked in that other post shows that you need to merge two arrays, one searching the titles and the other searching the meta_query.

    http://wordpress.stackexchange.com/questions/78649/using-meta-query-meta-query-with-a-search-query-s

    I’m just wondering if this is beyond the scope of adjusting the search that happens for relationship item types in ACF. It seems like I can only manipulate the $args, and not the query itself, in order to merge two queries.

  • You’re correct, there isn’t a way to do two queries and merge the results for the search in a relationship field. To do that you’d need to rebuild the relationship field code to do exactly what you want to do, perhaps create a new field type.

  • If someone is looking for a solution to this problem is to recommend the plugin:
    https://wordpress.org/plugins/acf-better-search/

    This plugin adds to default WordPress search engine the ability to search by content from selected fields of Advanced Custom Fields plugin.

    Everything works automatically, no need to add any additional code.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Search Relational Items by their ACF Fields’ is closed to new replies.