Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/03/2014 08:51:44 PM (11 years ago)
Author:
djpaul
Message:

Core: adjust the SQL generated by BP_User_Query's search_wildcard parameter for better Suggestions API search results.

Introduced in r8675, the search_wildcard parameter controls where BuddyPress places MySQL wildcard characters around the search term (when it's set).

This change adjusts how the wildcard SQL is constructed; in addition to the wildcard, we now also check for matches that have a single space character at the opposite end of the string (from where the wildcard is). This allows searching data that looks like (for example) a surname in a combined first name/surname field.
e.g. if a displayname is "Paul Gibbs", you can search for "Gi", and with search_wildcard=right, the SQL generated will be LIKE 'Gi%' OR LIKE '% Gi%'.

See #3278

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-classes.php

    r8677 r8753  
    369369        // xprofile field matches happen in bp_xprofile_bp_user_query_search()
    370370        if ( false !== $search_terms ) {
    371             $search_terms_like = bp_esc_like( $search_terms );
     371            $search_terms = bp_esc_like( $search_terms );
    372372
    373373            if ( $search_wildcard === 'left' ) {
    374                 $search_terms_like = '%' . $search_terms_like;
     374                $search_terms_nospace = '%' . $search_terms;
     375                $search_terms_space   = '%' . $search_terms . ' %';
    375376            } elseif ( $search_wildcard === 'right' ) {
    376                 $search_terms_like = $search_terms_like . '%';
     377                $search_terms_nospace =        $search_terms . '%';
     378                $search_terms_space   = '% ' . $search_terms . '%';
    377379            } else {
    378                 $search_terms_like = '%' . $search_terms_like . '%';
    379             }
    380 
    381             $sql['where']['search'] = $wpdb->prepare( "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_nicename LIKE %s ) )", $search_terms_like, $search_terms_like );
     380                $search_terms_nospace = '%' . $search_terms . '%';
     381                $search_terms_space   = '%' . $search_terms . '%';
     382            }
     383
     384            $sql['where']['search'] = $wpdb->prepare(
     385                "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s ) )",
     386                $search_terms_nospace,
     387                $search_terms_space,
     388                $search_terms_nospace,
     389                $search_terms_space
     390            );
    382391        }
    383392
     
    25672576     */
    25682577    protected $default_args = array(
    2569         'limit'        => 16,
     2578        'limit'        => 10,
    25702579        'only_friends' => false,
    25712580        'term'         => '',
     
    26072616            'per_page'        => $this->args['limit'],
    26082617            'search_terms'    => $this->args['term'],
     2618            'search_wildcard' => is_rtl() ? 'left' : 'right',
    26092619        );
    26102620
Note: See TracChangeset for help on using the changeset viewer.