Support

Account

Home Forums General Issues Search in the relationship field with text

Solving

Search in the relationship field with text

  • Hello!

    I have a page where I use multiple relationship fields.
    I need to be able to search after the title of the pages I set in the relationship fields.

    If I select the page with the title ‘My Page’ in the relationship field, I need to be able to search for ‘My Page’ and get a hit on that page. I only need to search in the title.

    I have a plugin which makes WP_Query search in all the meta data.

    The trouble is, only the ids are saved in the wp_postsmeta, so I get no hit on ‘My Page’, only on the ID for that page.

    I am thinking of, in the acf/update_value/name=… filter, saving a seperate row in the postmeta table which would contain all the information I need to be searchable.

    Does anyone have any thoughts on this?

  • You are trying do a search and return posts that have a related post that has a specific title. This will not work. It would be like searching for posts that have a related post where the related post has a specific meta value.

    The only way that you can do this is to make the information you want to search for part of the post that your searching for it in. This will require that you create another custom field that can hold the title of the related post.

    
    add_filter('acf/update_value/name=relationship_field', 'my_add_titles_to_post_for_search', 10, 3);
    function my_add_titles_to_post_for_search($value, $post_id, $field)( {
      // use a new field, it does not need to be an acf field
      // first delete anything it might hold
      delete_post_meta($post_id, '_hidden_relationship_title');
      if (!empty($value)) {
        $posts = $value;
        if (!is_array($posts)) {
          $posts = array($posts);
        }
        foreach ($posts as $post) {
          // add each related post's title
          add_post_meta($post_id, '_hidden_relationship_title', get_the_title($post), false);
        }
      }
      return $value;
    }
    
  • Thanks, John!

    This was similar to how I solved it yesterday 🙂

    I got it almost working, but I need this on 5 relationship fields on the page.
    I’ve tried to add the filter 1 time for each realtionship field, but then it seems like not all of them are run.

    Maybe 5 are too many.

    I tried this:

    add_filter('acf/update_value/name= ...
    add_filter('acf/update_value/name= ...
    add_filter('acf/update_value/name= ...
    add_filter('acf/update_value/name= ...
    add_filter('acf/update_value/name= ...
  • I fixed it now!

    I changed the 5 lines to this:
    add_filter('acf/update_value/type=relationship' ....

    I also saw you used get_the_title(), which is a better idea than what I was doing, which was get_post().

    Now it works, and the values I need are saved in the database.

    Also, since I have 5 different fields per post, I use the $field['_name'] plus a string to get seperate a meta_key per relationship field per post.

  • 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.

  • This is good. Would this automatically add fields on save? Anyway to bulk implement? Also I have a field for “part#” acf field. Any suggestions I’d be grateful. Gobble Gobble.

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

The topic ‘Search in the relationship field with text’ is closed to new replies.