Support

Account

Home Forums ACF PRO Customizing the Post Relationship search field…

Solved

Customizing the Post Relationship search field…

  • Is it possible to modify which fields the Post Relationship search box searches (in admin)?

    By default it seems to search both title and post content, but on a site with many hundreds of posts that often returns a super long posts list. I’d like to modify it so that it only search by post title, not the_content.

    Possible?

  • When performing the search ACF uses the standard WP search feature.

    You would need to add a filter https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/

    In this filter you would add a second filter following this https://stackoverflow.com/questions/9468804/make-wordpress-search-only-in-post-title

    
    add_filter('acf/fields/relationship/query/name={YOUR FIELD NAME}', 'my_acf_fields_relationship_query', 10, 3);
    function my_acf_fields_relationship_query( $args, $field, $post_id ) {
      add_filter('posts_search', '__search_by_title_only', 500, 2);
      return $args;
    }
    function __search_by_title_only($search, &$wp_query) {
      // code from stack
    }
    
  • Thanks John! This looks perfect. I’ll give it a try first thing tomorrow.

  • Nevermind. It worked perfectly. But you definitely have to use the field name, not the field ID. I thought the latter would work better since the field was nested in a repeater. Nope. 🙂

    Thanks again.

  • You can use the field name or field key, the ID is not useful. Inside a repeater I would suggest using the field key, sometimes the field name does not work.

    Your issue was here

    
    acf/fields/relationship/query/name=field_5ecd3b01eaf09
    

    to use the key is should be

    
    acf/fields/relationship/query/key=field_5ecd3b01eaf09
    
  • Your issue was here

    Yep, you’re right. Fixed that and it works perfectly with the key. Thanks again! 🙂

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.