Bp_Search_Members::sql( string $search_term, boolean $only_totalrow_count = false )

Generates sql for members search.

Description

Parameters

$search_term

(Required)

$only_totalrow_count

(Optional)

Default value: false

Return

(string) sql query

Source

File: bp-search/classes/class-bp-search-members.php

		public function sql( $search_term, $only_totalrow_count=false ){
			global $wpdb, $bp;

			$bp_prefix = bp_core_get_table_prefix();

			$query_placeholder = array();

			$COLUMNS = " SELECT ";

			if( $only_totalrow_count ){
				$COLUMNS .= " COUNT( DISTINCT u.id ) ";
			} else {
				$COLUMNS .= " DISTINCT u.id, 'members' as type, u.display_name LIKE %s AS relevance, a.date_recorded as entry_date ";
				$query_placeholder[] = '%'. $search_term .'%';
			}

			$FROM = " {$wpdb->users} u LEFT JOIN {$bp->members->table_name_last_activity} a ON a.user_id=u.id AND a.component = 'members' AND a.type = 'last_activity'";

			$WHERE = array();
			$WHERE[] = "1=1";
			$WHERE[] = "u.user_status = 0";
			$where_fields = array();

			/* ++++++++++++++++++++++++++++++++
			 * wp_users table fields
			 +++++++++++++++++++++++++++++++ */
			$user_fields = bp_get_search_user_fields();
			if ( ! empty( $user_fields ) ) {
				$conditions_wp_user_table = array();
				foreach ( $user_fields as $user_field => $field_label ) {


					if ( ! bp_is_search_user_field_enable( $user_field ) ) {
						continue;
					}

					if ( 'user_meta' === $user_field ) {
						//Search in user meta table for terms
						$conditions_wp_user_table[] = " ID IN ( SELECT user_id FROM {$wpdb->usermeta} WHERE {$bp_prefix}bp_strip_tags(meta_value) LIKE %s ) ";
						$query_placeholder[]        = '%' . $search_term . '%';
					} else {
						$conditions_wp_user_table[] = $user_field . " LIKE %s ";
						$query_placeholder[]        = '%' . $search_term . '%';
					}

				}

				if ( ! empty( $conditions_wp_user_table ) ) {

					$clause_wp_user_table = "u.id IN ( SELECT ID FROM {$wpdb->users}  WHERE ( ";
					$clause_wp_user_table .= implode( ' OR ', $conditions_wp_user_table );
					$clause_wp_user_table .= " ) ) ";

					$where_fields[] = $clause_wp_user_table;
				}

			}
			/* _____________________________ */

			/* ++++++++++++++++++++++++++++++++
			 * xprofile fields
			 +++++++++++++++++++++++++++++++ */
			//get all selected xprofile fields
			if( function_exists( 'bp_is_active' ) && bp_is_active( 'xprofile' ) ){
				$groups = bp_xprofile_get_groups( array(
					'fetch_fields' => true
				) );

				if ( !empty( $groups ) ){
					$selected_xprofile_fields = array(
						'word_search'   => array(0), //Search for whole word in field of type checkbox and radio
						'char_search'   => array(0), //Search for character in field of type textbox, textarea and etc
					);

					$word_search_field_type = array( 'radio', 'checkbox' );

					foreach ( $groups as $group ){
						if ( !empty( $group->fields ) ){
							foreach ( $group->fields as $field ) {
								if ( bp_is_search_xprofile_enable( $field->id ) ) {

									if( in_array( $field->type, $word_search_field_type ) ) {
										$selected_xprofile_fields['word_search'][] = $field->id;
									} else {
										$selected_xprofile_fields['char_search'][] = $field->id;
									}
								}
							}
						}
					}

					if( !empty( $selected_xprofile_fields ) ){
						//u.id IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s )
						$clause_xprofile_table = "u.id IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE ( {$bp_prefix}bp_strip_tags(value) LIKE %s AND field_id IN ( ";
						$clause_xprofile_table .= implode( ',', $selected_xprofile_fields['char_search'] );
						$clause_xprofile_table .= ") ) OR ( value REGEXP '[[:<:]]{$search_term}[[:>:]]' AND field_id IN ( ";
						$clause_xprofile_table .= implode( ',', $selected_xprofile_fields['word_search'] );
						$clause_xprofile_table .= ") ) ) ";

						$where_fields[] = $clause_xprofile_table;
						$query_placeholder[] = '%'. $search_term .'%';
					}
				}
			}
			/* _____________________________ */

                        /* ++++++++++++++++++++++++++++++++
			 * Search from search string
			 +++++++++++++++++++++++++++++++ */

                            $split_search_term = explode(' ', $search_term);

                            if (count($split_search_term) > 1 ) {

                                $clause_search_string_table = "u.id IN ( SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'bbgs_search_string' AND (";

                                foreach ( $split_search_term as $k => $sterm ) {

                                    if ( $k == 0 ) {
                                        $clause_search_string_table .= " meta_value LIKE %s ";
										$query_placeholder[] = '%'. $sterm .'%';
                                    } else {
                                        $clause_search_string_table .= " OR meta_value LIKE %s ";
										$query_placeholder[] = '%'. $sterm .'%';
                                    }
                                }

	                            $clause_search_string_table .= ") ) ";

                                $where_fields[] = $clause_search_string_table;

                            }

                        /* _____________________________ */

			if( !empty( $where_fields ) )
				$WHERE[] = '(' . implode ( ' OR ', $where_fields ) . ')';

			// other conditions
//			$WHERE[] = " a.component = 'members' ";
//			$WHERE[] = " a.type = 'last_activity' ";

			$sql = $COLUMNS . ' FROM ' . $FROM . ' WHERE ' . implode( ' AND ', $WHERE );
			if( !$only_totalrow_count ){
				$sql .= " GROUP BY u.id ";
			}

			$sql = $wpdb->prepare( $sql, $query_placeholder );


                        return apply_filters(
                            'Bp_Search_Members_sql',
                            $sql,
                            array(
                                'search_term'           => $search_term,
                                'only_totalrow_count'   => $only_totalrow_count,
                            )
                        );
		}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.