BP_Notifications_Notification::get_grouped_notifications_for_user( int $user_id )

Get a user’s unread notifications, grouped by component and action.

Description Description

Multiple notifications of the same type (those that share the same component_name and component_action) are collapsed for formatting as "You have 5 pending friendship requests", etc. See bp_notifications_get_notifications_for_user(). For a full-fidelity list of user notifications, use bp_notifications_get_all_notifications_for_user().

Top ↑

Parameters Parameters


(int) (Required) ID of the user whose notifications are being fetched.

Top ↑

Return Return

(array) Notifications items for formatting into a list.

Source Source

File: bp-notifications/classes/class-bp-notifications-notification.php

	public static function get_grouped_notifications_for_user( $user_id ) {
		global $wpdb;

		// Load BuddyPress.
		$bp = buddypress();

		// SELECT.
		$select_sql = "SELECT id, user_id, item_id, secondary_item_id, component_name, component_action, date_notified, is_new, COUNT(id) as total_count ";

		// FROM.
		$from_sql = "FROM {$bp->notifications->table_name} n ";

		// WHERE.
		$where_sql = self::get_where_sql( array(
			'user_id'        => $user_id,
			'is_new'         => 1,
			'component_name' => bp_notifications_get_registered_components(),
		), $select_sql, $from_sql );

		// GROUP
		$group_sql = "GROUP BY user_id, component_name, component_action";

		// SORT
		$order_sql = "ORDER BY date_notified desc";

		// Concatenate query parts.
		$sql = "{$select_sql} {$from_sql} {$where_sql} {$group_sql} {$order_sql}";

		// Return the queried results.
		return $wpdb->get_results( $sql );

Top ↑

Changelog Changelog

Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.