Support

Account

Home Forums ACF PRO Modify acf relationship search query

Helping

Modify acf relationship search query

  • Hi,
    I need to expand the search in relationship query to search for both title or custom field named “reference”. This is my functions.php but not work 🙁

    function modify_acf_relationship_search_query ($args, $field, $post ) {
    $args[‘meta_query’] = array(
    array(
    ‘key’=> ‘reference’,
    ‘value’=> $args[‘s’],
    ‘compare’ => ‘LIKE’
    )
    );
    return $args;
    }

    add_filter(‘acf/fields/relationship/query’, ‘modify_acf_relationship_search_query’, 10, 3);

    I can also serve a search by title and post ID (the reference is the post id followed by a letter)

    Can someone help me?

    (Sorry for my english)

  • In order to change this you need to add a pre_get_posts filter that will run for the query.

    Here is one solution for modifying the search https://wordpress.stackexchange.com/questions/99849/search-that-will-look-in-custom-field-post-title-and-post-content

    The best way to add the filter would be something like this

    
    add_filter('acf/fields/post_object_query/name=your_field_name', 'add_my_pre_get_posts_filter', 20, 3);
    function add_my_pre_get_posts_filter($args, $field, $post_id) {
      // this will cause the pre_get_posts filter to be added only for this query
      // add filters that will modify the queries here
    }
    
    // this is an example of one filters used to make the needed changes
    function some_function_name($args) {
      // remove this filter so it will not be run on subsequent queries
      // it is called with the same arguments you used to add this filter
      remove_filter('the_hook_for_this_filter', 'some_function_name');
    
      // the rest of the filter here
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Modify acf relationship search query’ is closed to new replies.