Bp_Search_Groups::sql( $search_term,  $only_totalrow_count = false )

Description

Source

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

		public function sql( $search_term, $only_totalrow_count=false ){
			/* an example UNION query :-
			-----------------------------------------------------
			(
				SELECT
					DISTINCT g.id, 'groups' as type, g.name LIKE '%ho%' AS relevance, gm2.meta_value as entry_date
				FROM
					wp_bp_groups_groupmeta gm1, wp_bp_groups_groupmeta gm2, wp_bp_groups g
				WHERE
					1=1
					AND g.id = gm1.group_id
					AND g.id = gm2.group_id
					AND gm2.meta_key = 'last_activity'
					AND gm1.meta_key = 'total_member_count'
					AND ( g.name LIKE '%ho%' OR g.description LIKE '%ho%' )
			)
			----------------------------------------------------
			*/
			global $wpdb, $bp;
			$query_placeholder = array();

			$sql = " SELECT ";

			if ( $only_totalrow_count ) {
				$sql .= " COUNT( DISTINCT g.id ) ";
			} else {
				$sql .= " DISTINCT g.id, 'groups' as type, g.name LIKE %s AS relevance, gm2.meta_value as entry_date ";
				$query_placeholder[] = '%'.$wpdb->esc_like( $search_term ).'%';
			}

			$sql .= " FROM 
						{$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name} g 
					WHERE 
						1=1 
						AND g.id = gm1.group_id 
						AND g.id = gm2.group_id 
						AND gm2.meta_key = 'last_activity' 
						AND gm1.meta_key = 'total_member_count' 
						AND ( g.name LIKE %s OR g.description LIKE %s )
				";
			$query_placeholder[] = '%'.$wpdb->esc_like( $search_term ).'%';
			$query_placeholder[] = '%'.$wpdb->esc_like( $search_term ).'%';

			/** LOCATION AUTOCOMPLETE SEARCH ************************************************/

			if (function_exists('bp_bpla') && 'yes' == bp_bpla()->option('enable-for-groups') ) {

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

					$sql .= "OR g.id IN ( SELECT group_id FROM {$bp->groups->table_name_groupmeta} WHERE meta_key = 'bbgs_group_search_string' AND ";

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

						if ( $k == 0 ) {
							$sql .= "meta_value LIKE %s";
							$query_placeholder[] = '%'.$wpdb->esc_like( $sterm ) . '%';
						} else {
							$sql .= "AND meta_value LIKE %s";
							$query_placeholder[] = '%'.$wpdb->esc_like( $sterm ) .'%';
						}

					}
					$sql .= " ) ";

			}

			/**
			 * Properly handle hidden groups.
			 * For guest users - exclude all hidden groups.
			 * For members - include only those hidden groups where current user is a member.
			 * For admins - include all hidden groups ( do nothing extra ).
			 * @since 1.1.0
			 */
			if( is_user_logged_in() ){
				if( !current_user_can( 'level_10' ) ){
					//get all hidden groups where i am a member of
					$hidden_groups_sql = $wpdb->prepare( "SELECT DISTINCT gm.group_id FROM {$bp->groups->table_name_members} gm JOIN {$bp->groups->table_name} g ON gm.group_id = g.id WHERE gm.user_id = %d AND gm.is_confirmed = 1 AND gm.is_banned = 0 AND g.status='hidden' ", bp_loggedin_user_id() );
					$hidden_groups_ids = $wpdb->get_col( $hidden_groups_sql );
					if( empty( $hidden_groups_ids ) ){
						$hidden_groups_ids = array( 99999999 );//arbitrarily large number
					}

					$hidden_groups_ids_csv = implode( ',', $hidden_groups_ids );

					//either gruops which are not hidden,
					//or if hidden, only those where i am a member.
					$sql .= " AND ( g.status != 'hidden' OR g.id IN ( {$hidden_groups_ids_csv} ) ) ";
				}
			} else {
				$sql .= " AND g.status != 'hidden' ";
			}

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

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

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.