Skip to content

Instantly share code, notes, and snippets.

@sbrajesh

sbrajesh/part2 Secret

Created August 19, 2019 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrajesh/d71d88317c649333b9e456bf3ba8cf4f to your computer and use it in GitHub Desktop.
Save sbrajesh/d71d88317c649333b9e456bf3ba8cf4f to your computer and use it in GitHub Desktop.
/**
* Get notification for use specific to a component.
*
* @param int $user_id user id.
* @param string $component_name component name.
*
* @return array|object|null
*/
function bd_custom_get_grouped_notifications_for_user( $user_id, $component_name ) {
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 = $wpdb->prepare( "WHERE component_name = %s AND user_id = %d AND is_new = %d", $component_name, $user_id, 1 );
// 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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment