Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Filter activities by friends and groups

@richtelford

Participant

After researching a bit further I think I could easily achieve what I’d like if only this function (in bp-activity-classes.php line 1186):


/**
	 * Create filter SQL clauses.
	 *
	 * @since BuddyPress (1.5.0)
	 *
	 * @param array $filter_array {
	 *     Fields and values to filter by.
	 *     @type array|string|id $user_id User ID(s).
	 *     @type array|string $object Corresponds to the 'component'
	 *           column in the database.
	 *     @type array|string $action Corresponds to the 'type' column
	 *           in the database.
	 *     @type array|string|int $primary_id Corresponds to the 'item_id'
	 *           column in the database.
	 *     @type array|string|int $secondary_id Corresponds to the
	 *           'secondary_item_id' column in the database.
	 *     @type int $offset Return only those items with an ID greater
	 *           than the offset value.
	 *     @type string $since Return only those items that have a
	 *           date_recorded value greater than a given MySQL-formatted
	 *           date.
	 * }
	 * @return string The filter clause, for use in a SQL query.
	 */
	public static function get_filter_sql( $filter_array ) {

		$filter_sql = array();

		if ( !empty( $filter_array['user_id'] ) ) {
			$user_sql = BP_Activity_Activity::get_in_operator_sql( 'a.user_id', $filter_array['user_id'] );
			if ( !empty( $user_sql ) )
				$filter_sql[] = $user_sql;
		}

		if ( !empty( $filter_array['object'] ) ) {
			$object_sql = BP_Activity_Activity::get_in_operator_sql( 'a.component', $filter_array['object'] );
			if ( !empty( $object_sql ) )
				$filter_sql[] = $object_sql;
		}

		if ( !empty( $filter_array['action'] ) ) {
			$action_sql = BP_Activity_Activity::get_in_operator_sql( 'a.type', $filter_array['action'] );
			if ( ! empty( $action_sql ) )
				$filter_sql[] = $action_sql;
		}

		if ( !empty( $filter_array['primary_id'] ) ) {
			$pid_sql = BP_Activity_Activity::get_in_operator_sql( 'a.item_id', $filter_array['primary_id'] );
			if ( !empty( $pid_sql ) )
				$filter_sql[] = $pid_sql;
		}

		if ( !empty( $filter_array['secondary_id'] ) ) {
			$sid_sql = BP_Activity_Activity::get_in_operator_sql( 'a.secondary_item_id', $filter_array['secondary_id'] );
			if ( !empty( $sid_sql ) )
				$filter_sql[] = $sid_sql;
		}

		if ( ! empty( $filter_array['offset'] ) ) {
			$sid_sql = absint( $filter_array['offset'] );
			$filter_sql[] = "a.id >= {$sid_sql}";
		}

		if ( ! empty( $filter_array['since'] ) ) {
			// Validate that this is a proper Y-m-d H:i:s date
			// Trick: parse to UNIX date then translate back
			$translated_date = date( 'Y-m-d H:i:s', strtotime( $filter_array['since'] ) );
			if ( $translated_date === $filter_array['since'] ) {
				$filter_sql[] = "a.date_recorded > '{$translated_date}'";
			}
		}

		if ( empty( $filter_sql ) )
			return false;

		return join( ' AND ', $filter_sql );
	}

would allow filters to be combined with OR rather than AND. That way I could easily filter on a bunch of user_ids and group_ids which would get me the results I’m after. I might try extending the class and overriding that function. Watch this space!

Skip to toolbar