Support

Account

Home Forums Feature Requests AJAX search threshold

Helping

AJAX search threshold

    • scimon

    • October 30, 2013 at 1:42 am

    Would it be possible to switch the AJAX search for relationships to not automatically submit requests until there are at least three characters typed? This is a pretty standard convention that reduces server load by avoiding submission of overly broad queries, and actually results in a more responsive interface for anyone with a decent typing speed. For example, the WordPress tag input field does this. You could still allow explicit searching of smaller strings by hitting enter…

    Thanks in advance!

    P.S. This plugin is quite well designed and documented. I’ve been able to add several features (inclusion of slugs in the results list, and searching by slugs) without too much effort; searching required mimicry/abuse of the like_title functionality though, and would break if the like_title variable name ever changed… Documenting this below in case anybody else wants a recipe to search by slug, which is not as obvious as adding the slug to the results. As coded, this requires that you prepend your search with an = in order to search by slug, but you can change the escaped symbol in the preg_match to something else you prefer e.g; ~ or !

    function field_relationship($args, $field, $post){
      if( preg_match('/^\=(.+)/', $args['like_title'], $match) ){
        unset($args['like_title']); #Prevent search by title
        $args['is_slug'] = $match[1];
        add_filter( 'posts_where', 'slug_posts_where', 10, 2 );
      }
    
      return $args;
    }
    add_filter('acf/fields/relationship/query/name=field', 'field_relationship', 10, 4);
    function slug_posts_where( $where, &$wp_query ){
      global $wpdb;
    
      if( $slug = $wp_query->get('is_slug') ){
        $where .= " AND " . $wpdb->posts . ".post_name LIKE '" . esc_sql( like_escape($slug)) . "'";
      }
      return $where;
    }
    
    • Elliot

    • October 30, 2013 at 4:44 pm

    Hi @scimon

    Thanks for the feedback. I’ll see what I can do.

    Thanks
    E

Viewing 2 posts - 1 through 2 (of 2 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.