Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/03/2014 08:51:44 PM (10 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-xprofile/bp-xprofile-functions.php

    r8689 r8753  
    591591    $bp = buddypress();
    592592
    593     $search_terms_clean = esc_sql( esc_sql( $query->query_vars['search_terms'] ) );
     593    $search_terms_clean = bp_esc_like( $query->query_vars['search_terms'] );
    594594
    595595    if ( $query->query_vars['search_wildcard'] === 'left' ) {
    596         $search_terms_clean = '%' . $search_terms_clean;
     596        $search_terms_nospace = '%' . $search_terms_clean;
     597        $search_terms_space   = '%' . $search_terms_clean . ' %';
    597598    } elseif ( $query->query_vars['search_wildcard'] === 'right' ) {
    598         $search_terms_clean = $search_terms_clean . '%';
     599        $search_terms_nospace =        $search_terms_clean . '%';
     600        $search_terms_space   = '% ' . $search_terms_clean . '%';
    599601    } else {
    600         $search_terms_clean = '%' . $search_terms_clean . '%';
     602        $search_terms_nospace = '%' . $search_terms_clean . '%';
     603        $search_terms_space   = '%' . $search_terms_clean . '%';
    601604    }
    602605
    603606    // Combine the core search (against wp_users) into a single OR clause
    604607    // with the xprofile_data search
     608    $search_xprofile = $wpdb->prepare(
     609        "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s )",
     610        $search_terms_nospace,
     611        $search_terms_space
     612    );
     613
    605614    $search_core     = $sql['where']['search'];
    606     $search_xprofile = "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '{$search_terms_clean}' )";
    607615    $search_combined = "( {$search_xprofile} OR {$search_core} )";
    608 
    609616    $sql['where']['search'] = $search_combined;
    610617
Note: See TracChangeset for help on using the changeset viewer.