Support

Account

Home Forums ACF PRO Search in relationship field

Solving

Search in relationship field

  • Hello,
    Im using relationship field in pages (called content_boxes).
    In this relationship field you chose custom posts (called content_box).

    For example – I have page About Us – i choose there as relationship few posts – story, team, location.

    On the page About Us i show contents of story, team, location posts.

    What I need to do is to search (in front end search, standard WP search) in CONTENT of those related posts (not the tittles).

    So if I search for string – great team it should find page About Us, because it has this string in Team custom post (which is related to page About Us).

    Is it possible?

  • Is it possible?

    No, there isn’t any way to get the standard wp search to search additional posts from a relationship.

  • Any search altering could do it? Somehow?

  • Even plugins that alter the WP search cannot do this. The problem is that the content you want to search is save to a different post. When WP searches it returns the post where the content is located.

    There may be a premium search application somewhere that might be able to do this, but I have never heard of one.

    The only way that this could be accomplished would be to copy the content from the related posts into the post you want to return the results for.

    For example:
    Please not that this is just an example, I do not know if this will meet your needs exactly or how it will interact with the rest of your site/code

    
    add_action('acf/save_post', 'copy_related_content', 20);
    function copy_related_content($post_id) {
      remove_filter('acf/save_post', 'copy_related_content', 20); // prevent infinite loop
      $posts = get_field('your-relationship-field', $post_id);
      if ($posts) {
        $content = '';
        foreach ($posts as $post) {
          $content .= ' '.$post->post_content;
        }
        $post = array(
          'ID' => $post_id,
          'post_content' => $content
        );
        $result = wp_update_post($post, true);
      }
      add_action('acf/save_post', 'copy_related_content', 20); // replace filter
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.