yanknudtskov
7/17/2014 - 10:17 AM

WordPress Snippet to modify the WordPress Search Function with custom fields

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' ) );