Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

As above guys, I'm trying to build an AJAX search that works within BuddyPress WITHOUT modifying core code, but that allows me to use a wildcard when I'm searching by username.

Any suggestions?

share|improve this question

1 Answer 1

I found the following class in the add user autocomplete plugin . It extends the normal searcha and allows '*';

Example: $wp_user_search = new A2B_User_Query( array( 'search' => $s . '*' ) ) ;

class A2B_User_Query extends WP_User_Query { /** * @see WP_User_Query::get_search_sql() */ function get_search_sql( $string, $cols, $wild = false ) { $string = esc_sql( $string );

    // Always search all columns
    $cols = array(
        'user_email',
        'user_login',
        'user_nicename',
        'user_url',
        'display_name'
    );

    // Always do 'both' for trailing_wild
    $wild = 'both';

    $searches = array();
    $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
    $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
    foreach ( $cols as $col ) {
        if ( 'ID' == $col )
            $searches[] = "$col = '$string'";
        else
            $searches[] = "$col LIKE '$leading_wild" . like_escape($string) . "$trailing_wild'";
    }

    return ' AND (' . implode(' OR ', $searches) . ')';
}

}

share|improve this answer
    
+1. I wrote that plugin. It's intended for use in the Dashboard, but it could easily be adapted for frontend use. –  Boone Gorges Mar 19 '12 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.