Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/09/2014 01:58:11 PM (10 years ago)
Author:
boonebgorges
Message:

BP_User_Query search_terms should match against user_login and user_nicename in addition to xprofile fields

This changeset changes the search_terms logic in BP_User_Query so that it
matches user_login and user_nicename in the core users table. Then, via filter,
the xprofile component amends the WHERE clause so that search_terms are matched
in the users table OR the xprofile data table.

Clauses have been refactored to subqueries rather than storing the matched
user IDs in PHP, for improved performance.

See #5155

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-functions.php

    r7885 r8087  
    455455
    456456/**
     457 * When search_terms are passed to BP_User_Query, search against xprofile fields.
     458 *
     459 * @since BuddyPress (2.0.0)
     460 *
     461 * @param array $sql Clauses in the user_id SQL query.
     462 * @param BP_User_Query User query object.
     463 */
     464function bp_xprofile_bp_user_query_search( $sql, BP_User_Query $query ) {
     465    global $wpdb;
     466
     467    if ( empty( $query->query_vars['search_terms'] ) || empty( $sql['where']['search'] ) ) {
     468        return $sql;
     469    }
     470
     471    $bp = buddypress();
     472
     473    $search_terms_clean = esc_sql( esc_sql( $query->query_vars['search_terms'] ) );
     474
     475    // Combine the core search (against wp_users) into a single OR clause
     476    // with the xprofile_data search
     477    $search_core     = $sql['where']['search'];
     478    $search_xprofile = "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '%{$search_terms_clean}%' )";
     479    $search_combined = "( {$search_xprofile} OR {$search_core} )";
     480
     481    $sql['where']['search'] = $search_combined;
     482
     483    return $sql;
     484}
     485add_action( 'bp_user_query_uid_clauses', 'bp_xprofile_bp_user_query_search', 10, 2 );
     486
     487/**
    457488 * Syncs Xprofile data to the standard built in WordPress profile data.
    458489 *
Note: See TracChangeset for help on using the changeset viewer.