BP_Notifications_Notification::get( array $args = array() )

Get notifications, based on provided filter parameters.

Description

Parameters

$args

(array) (Optional) Associative array of arguments. All arguments but $page and $per_page can be treated as filter values for get_where_sql() and get_query_clauses(). All items are optional.

  • 'id'
    (int|array) ID of notification being updated. Can be an array of IDs.
  • 'user_id'
    (int|array) ID of user being queried. Can be an array of user IDs.
  • 'item_id'
    (int|array) ID of associated item. Can be an array of multiple item IDs.
  • 'secondary_item_id'
    (int|array) ID of secondary associated item. Can be an array of multiple IDs.
  • 'component_name'
    (string|array) Name of the component to filter by. Can be an array of component names.
  • 'component_action'
    (string|array) Name of the action to filter by. Can be an array of actions.
  • 'is_new'
    (bool) Whether to limit to new notifications. True returns only new notifications, false returns only non-new notifications. 'both' returns all. Default: true.
  • 'search_terms'
    (string) Term to match against component_name or component_action fields.
  • 'order_by'
    (string) Database column to order notifications by.
  • 'sort_order'
    (string) Either 'ASC' or 'DESC'.
  • 'order_by'
    (string) Field to order results by.
  • 'sort_order'
    (string) ASC or DESC.
  • 'page'
    (int) Number of the current page of results. Default: false (no pagination - all items).
  • 'per_page'
    (int) Number of items to show per page. Default: false (no pagination - all items).
  • 'meta_query'
    (array) Array of meta_query conditions. See WP_Meta_Query::queries.
  • 'date_query'
    (array) Array of date_query conditions. See first parameter of WP_Date_Query::__construct().
  • 'update_meta_cache'
    (bool) Whether to update meta cache. Default: true.

Default value: array()

Return

(array) Located notifications.

Source

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

	public static function get( $args = array() ) {
		global $wpdb;

		// Parse the arguments.
		$r = self::parse_args( $args );

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

		// METADATA.
		$meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );

		// SELECT.
		$select_sql = "SELECT *";

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

		// JOIN.
		$join_sql   = $meta_query_sql['join'];

		// WHERE.
		$where_sql  = self::get_where_sql( array(
			'id'                => $r['id'],
			'user_id'           => $r['user_id'],
			'item_id'           => $r['item_id'],
			'secondary_item_id' => $r['secondary_item_id'],
			'component_name'    => $r['component_name'],
			'component_action'  => $r['component_action'],
			'is_new'            => $r['is_new'],
			'search_terms'      => $r['search_terms'],
			'date_query'        => $r['date_query']
		), $select_sql, $from_sql, $join_sql, $meta_query_sql );

		// ORDER BY.
		$order_sql  = self::get_order_by_sql( array(
			'order_by'   => $r['order_by'],
			'sort_order' => $r['sort_order']
		) );

		// LIMIT %d, %d.
		$pag_sql    = self::get_paged_sql( array(
			'page'     => $r['page'],
			'per_page' => $r['per_page']
		) );

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

		$results = $wpdb->get_results( $sql );

		// Integer casting.
		foreach ( $results as $key => $result ) {
			$results[$key]->id                = (int) $results[$key]->id;
			$results[$key]->user_id           = (int) $results[$key]->user_id;
			$results[$key]->item_id           = (int) $results[$key]->item_id;
			$results[$key]->secondary_item_id = (int) $results[$key]->secondary_item_id;
			$results[$key]->is_new            = (int) $results[$key]->is_new;
		}

		// Update meta cache.
		if ( true === $r['update_meta_cache'] ) {
			bp_notifications_update_meta_cache( wp_list_pluck( $results, 'id' ) );
		}

		return $results;
	}

Changelog

Changelog
Version Description
BuddyPress 1.9.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.