Support

Account

Home Forums Backend Issues (wp-admin) Narrow the search of relationship field types

Solving

Narrow the search of relationship field types

  • Currently the relationship field queries and does “LIKE” searches on title, content, etc. With 400,000 published posts in our wp_posts table, long searches take over 30seconds to execute. I would like to limit this search to only looking at the title field, not the content since our users know the title they want to find.

    currently I have done this:

    function acf_relationship_query($args, $field, $post) {
        $args['posts_per_page'] = 5;
        $args['post_status'] = 'publish';
            // return
        return $args;
    
    }
    add_filter('acf/fields/relationship/query/name=related_posts_1', 'acf_relationship_query', 10, 3);
    add_filter('acf/fields/relationship/query/name=book_callout', 'acf_relationship_query', 10, 3);

    removing the drafts from the results and limiting the amount returned.

  • searching titles and content is something that is part of WP and not something that is specific to ACF. Basically, ACF does a standard query with the ‘s’ parameter set. This can be altered with quite a bit of work, but I don’t know if you can limit it only to the query being done by the relationship field.

    I found some code here for removing the content from search. https://nathaningram.com/restricting-wordpress-search-to-titles-only/

    The problem is that I do not think there is anything that will identify the query as being for the relationship field.

  • I’m having this exact same issue.

    A couple hours of research on pre_get_post hasn’t given me a solution yet.

    I haven’t seen any code examples, but according to the Codex it might be solved by adding something like this to your acf_relationship_query query $args:

    $args['fields'] = 'post_title';

    This might not remove the existing filters, however. I’ve emailed ACF for support on this issue.

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

The topic ‘Narrow the search of relationship field types’ is closed to new replies.