bp_has_activities( array|string $args = '' )

Initialize the activity loop.

Description

Based on the $args passed, bp_has_activities() populates the $activities_template global, enabling the use of BuddyPress templates and template functions to display a list of activity items.

Parameters

$args

(array|string) (Optional) Arguments for limiting the contents of the activity loop. Most arguments are in the same format as BP_Activity_Activity::get(). However, because the format of the arguments accepted here differs in a number of ways, and because bp_has_activities() determines some default arguments in a dynamic fashion, we list all accepted arguments here as well. Arguments can be passed as an associative array, or as a URL querystring (eg, 'user_id=4&display_comments=threaded').

  • 'page'
    (int) Which page of results to fetch. Using page=1 without per_page will result in no pagination. Default: 1.
  • 'per_page'
    (int|bool) Number of results per page. Default: 20.
  • 'page_arg'
    (string) String used as a query parameter in pagination links. Default: 'acpage'.
  • 'max'
    (int|bool) Maximum number of results to return. Default: false (unlimited).
  • 'fields'
    (string) Activity fields to retrieve. 'all' to fetch entire activity objects, 'ids' to get only the activity IDs. Default 'all'.
  • 'count_total'
    (string|bool) If true, an additional DB query is run to count the total activity items for the query. Default: false.
  • 'sort'
    (string) 'ASC' or 'DESC'. Default: 'DESC'.
  • 'exclude'
    (array|bool) Array of activity IDs to exclude. Default: false.
  • 'in'
    (array|bool) Array of IDs to limit query by (IN). 'in' is intended to be used in conjunction with other filter parameters. Default: false.
  • 'include'
    (array|bool) Array of exact activity IDs to query. Providing an 'include' array will override all other filters passed in the argument array. When viewing the permalink page for a single activity item, this value defaults to the ID of that item. Otherwise the default is false.
  • 'meta_query'
    (array) Limit by activitymeta by passing an array of meta_query conditions. See WP_Meta_Query::queries for a description of the syntax.
  • 'date_query'
    (array) Limit by date by passing an array of date_query conditions. See first parameter of WP_Date_Query::__construct() for syntax.
  • 'filter_query'
    (array) Advanced activity filtering. See BP_Activity_Query::__construct().
  • 'search_terms'
    (string) Limit results by a search term. Default: false.
  • 'scope'
    (string) Use a BuddyPress pre-built filter. - 'just-me' retrieves items belonging only to a user; this is equivalent to passing a 'user_id' argument. - 'friends' retrieves items belonging to the friends of a user. - 'groups' retrieves items belonging to groups to which a user belongs to. - 'favorites' retrieves a user's favorited activity items. - 'mentions' retrieves items where a user has received an @-mention. The default value of 'scope' is set to one of the above if that value appears in the appropriate place in the URL; eg, 'scope' will be 'groups' when visiting <a href="http://example.com/members/joe/activity/groups/">http://example.com/members/joe/activity/groups/</a>. Otherwise defaults to false.
  • 'user_id'
    (int|array|bool) The ID(s) of user(s) whose activity should be fetched. Pass a single ID or an array of IDs. When viewing a user profile page (but not that user's activity subpages, ie My Connections, My Groups, etc), 'user_id' defaults to the ID of the displayed user. Otherwise the default is false.
  • 'object'
    (string|array|bool) Filters by the component column in the database, which is generally the component ID in the case of BuddyPress components, or the plugin slug in the case of plugins. For example, 'groups' will limit results to those that are associated with the BP Groups component. Accepts a single component string, or an array of multiple components. Defaults to 'groups' when viewing the page of a single group, the My Groups activity filter, or the Activity > Groups filter of a user profile. Otherwise defaults to false.
  • 'action'
    (string|array|bool) Filters by the type column in the database, which is a string categorizing the activity item (eg, 'new_blog_post', 'created_group'). Accepts a comma-delimited string or an array of types. Default: false.
  • 'primary_id'
    (int|array|bool) Filters by the item_id column in the database. The meaning of 'primary_id' differs between components/types; for example, in the case of 'created_group', 'primary_id' is the ID of the group. Accepts a single ID, or an array of multiple IDs. When viewing a single group, defaults to the current group ID. When viewing a user's Groups stream page, defaults to the IDs of the user's groups. Otherwise defaults to false.
  • 'secondary_id'
    (int|array|bool) Filters by the secondary_item_id column in the database. The meaning of 'secondary_id' differs between components/types. Accepts a single ID, or an array of multiple IDs. Defaults to false.
  • 'offset'
    (int) Return only activity items with an ID greater than or equal to this one. Note that providing an offset will disable pagination. Default: false.
  • 'display_comments'
    (string|bool) How to handle activity comments. Possible values: - 'threaded' - comments appear in a threaded tree, under their parent items. - 'stream' - the activity feed is presented in a flat manner, with comments sorted in chronological order alongside other activity items. - false - don't fetch activity comments at all. Default: 'threaded'.
  • 'show_hidden'
    (bool) Whether to show items marked hide_sitewide. Defaults to false, except in the following cases: - User is viewing his own activity feed. - User is viewing the activity feed of a non-public group of which he is a member.
  • 'spam'
    (string|bool) Spam status. 'ham_only', 'spam_only', or false to show all activity regardless of spam status. Default: 'ham_only'.
  • 'populate_extras'
    (bool) Whether to pre-fetch the activity metadata for the queried items. Default: true.

Default value: ''

Return

(bool) Returns true when activities are found, otherwise false.

Source

File: bp-activity/bp-activity-template.php

function bp_has_activities( $args = '' ) {
	global $activities_template;

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

	/*
	 * Smart Defaults.
	 */

	// User filtering.
	$user_id = bp_displayed_user_id()
		? bp_displayed_user_id()
		: false;

	$privacy  = array( 'public' );
	if ( is_user_logged_in() ) {
		$privacy[] = 'loggedin';
		if ( bp_is_active( 'friends' ) && ! bp_is_group() && ! bp_is_activity_directory() ) {
			$is_friend = friends_check_friendship( get_current_user_id(), $user_id );
			if( $is_friend ) {
				$privacy[] = 'friends';
			}
		} else if ( bp_is_active( 'friends' ) && bp_is_activity_directory() ) {
			$privacy[] = 'friends';
        }

		if ( bp_is_my_profile() ) {
			$privacy[] = 'onlyme';
		}
	}

	// Group filtering.
	if ( bp_is_group() ) {
		$object      = $bp->groups->id;
		$primary_id  = bp_get_current_group_id();
		$show_hidden = (bool) ( groups_is_user_member( bp_loggedin_user_id(), $primary_id ) || bp_current_user_can( 'bp_moderate' ) );
	} else {
		$object      = false;
		$primary_id  = false;
		$show_hidden = false;
	}

	// The default scope should recognize custom slugs.
	$scope = array_key_exists( bp_current_action(), (array) $bp->loaded_components )
		? $bp->loaded_components[ bp_current_action() ]
		: bp_current_action();

	// Support for permalinks on single item pages: /groups/my-group/activity/124/.
	$include = bp_is_current_action( bp_get_activity_slug() )
		? bp_action_variable( 0 )
		: false;

	$search_terms_default = false;
	$search_query_arg = bp_core_get_component_search_query_arg( 'activity' );
	if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
		$search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
	}

	/*
	 * Parse Args.
	 */

	// Note: any params used for filtering can be a single value, or multiple
	// values comma separated.
	$r = bp_parse_args( $args, array(
		'display_comments'  => 'threaded',   // False for none, stream/threaded - show comments in the stream or threaded under items.
		'include'           => $include,     // Pass an activity_id or string of IDs comma-separated.
		'exclude'           => false,        // Pass an activity_id or string of IDs comma-separated.
		'in'                => false,        // Comma-separated list or array of activity IDs among which to search.
		'sort'              => 'DESC',       // Sort DESC or ASC.
		'page'              => 1,            // Which page to load.
		'per_page'          => 20,           // Number of items per page.
		'page_arg'          => 'acpage',     // See https://buddypress.trac.wordpress.org/ticket/3679.
		'max'               => false,        // Max number to return.
		'fields'            => 'all',
		'count_total'       => false,
		'show_hidden'       => $show_hidden, // Show activity items that are hidden site-wide?
		'spam'              => 'ham_only',   // Hide spammed items.

		// Scope - pre-built activity filters for a user (friends/groups/favorites/mentions).
		'scope'             => $scope,

		// Filtering
		'user_id'           => $user_id,     // user_id to filter on.
		'object'            => $object,      // Object to filter on e.g. groups, profile, status, friends.
		'action'            => false,        // Action to filter on e.g. activity_update, profile_updated.
		'primary_id'        => $primary_id,  // Object ID to filter on e.g. a group_id or blog_id etc.
		'secondary_id'      => false,        // Secondary object ID to filter on e.g. a post_id.
		'offset'            => false,        // Return only items >= this ID.
		'since'             => false,        // Return only items recorded since this Y-m-d H:i:s date.
		'privacy'           => $privacy,     // privacy to filter on - public, onlyme, loggedin, friends, media.

		'meta_query'        => false,        // Filter on activity meta. See WP_Meta_Query for format.
		'date_query'        => false,        // Filter by date. See first parameter of WP_Date_Query for format.
		'filter_query'      => false,        // Advanced filtering.  See BP_Activity_Query for format.

		// Searching.
		'search_terms'      => $search_terms_default,
		'update_meta_cache' => true,
	), 'has_activities' );

	/*
	 * Smart Overrides.
	 */

	// Translate various values for 'display_comments'
	// This allows disabling comments via ?display_comments=0
	// or =none or =false. Final true is a strict type check. See #5029.
	if ( in_array( $r['display_comments'], array( 0, '0', 'none', 'false' ), true ) ) {
		$r['display_comments'] = false;
	}

	// Ignore pagination if an offset is passed.
	if ( ! empty( $r['offset'] ) ) {
		$r['page'] = 0;
	}

	// Search terms.
	if ( ! empty( $_REQUEST['s'] ) && empty( $r['search_terms'] ) ) {
		$r['search_terms'] = $_REQUEST['s'];
	}

	// Do not exceed the maximum per page.
	if ( ! empty( $r['max'] ) && ( (int) $r['per_page'] > (int) $r['max'] ) ) {
		$r['per_page'] = $r['max'];
	}

	/**
	 * Filters whether BuddyPress should enable afilter support.
	 *
	 * Support for basic filters in earlier BP versions is disabled by default.
	 * To enable, put add_filter( 'bp_activity_enable_afilter_support', '__return_true' );
	 * into bp-custom.php or your theme's functions.php.
	 *
	 * @since BuddyPress 1.6.0
	 *
	 * @param bool $value True if BuddyPress should enable afilter support.
	 */
	if ( isset( $_GET['afilter'] ) && apply_filters( 'bp_activity_enable_afilter_support', false ) ) {
		$r['filter'] = array(
			'object' => $_GET['afilter']
		);
	} elseif ( ! empty( $r['user_id'] ) || ! empty( $r['object'] ) || ! empty( $r['action'] ) || ! empty( $r['primary_id'] ) || ! empty( $r['secondary_id'] ) || ! empty( $r['offset'] ) || ! empty( $r['since'] ) ) {
		$r['filter'] = array(
			'user_id'      => $r['user_id'],
			'object'       => $r['object'],
			'action'       => $r['action'],
			'primary_id'   => $r['primary_id'],
			'secondary_id' => $r['secondary_id'],
			'offset'       => $r['offset'],
			'since'        => $r['since']
		);
	} else {
		$r['filter'] = false;
	}

	// If specific activity items have been requested, override the $hide_spam
	// argument. This prevents backpat errors with AJAX.
	if ( ! empty( $r['include'] ) && ( 'ham_only' === $r['spam'] ) ) {
		$r['spam'] = 'all';
	}

	/*
	 * Query
	 */

	$activities_template = new BP_Activity_Template( $r );

	/**
	 * Filters whether or not there are activity items to display.
	 *
	 * @since BuddyPress 1.1.0
	 *
	 * @param bool   $value               Whether or not there are activity items to display.
	 * @param string $activities_template Current activities template being used.
	 * @param array  $r                   Array of arguments passed into the BP_Activity_Template class.
	 */
	return apply_filters( 'bp_has_activities', $activities_template->has_activities(), $activities_template, $r );
}

Changelog

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