Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created July 17, 2014 10:17
Show Gist options
  • Save yanknudtskov/151325752a3db718027b to your computer and use it in GitHub Desktop.
Save yanknudtskov/151325752a3db718027b to your computer and use it in GitHub Desktop.
WordPress Snippet to modify the WordPress Search Function with custom fields
public function va_extended_user_search( $user_query )
{
// Make sure this is only applied to user search
if ( $user_query->query_vars['search'] ){
$search = trim( $user_query->query_vars['search'], '*' );
if ( $_REQUEST['s'] == $search ){
global $wpdb;
$user_query->query_from .= " JOIN ".$wpdb->prefix ."usermeta MF ON MF.user_id = {$wpdb->users}.ID AND MF.meta_key = 'META_KEY_NAME'";
$user_query->query_where = 'WHERE 1=1' . $user_query->get_search_sql( $search, array( 'user_login', 'user_email', 'user_nicename', 'MF.meta_value'), 'both' );
}
}
}
add_action( 'pre_user_query', array($this, 'va_extended_user_search' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment