bp_has_albums( array|string $args = '' )

Initialize the album loop.

Description

Based on the $args passed, bp_has_albums() populates the $media_album_template global, enabling the use of BuddyPress templates and template functions to display a list of media album items.

Parameters

$args

(Optional) Arguments for limiting the contents of the media loop. Most arguments are in the same format as BP_Media_Album::get(). However, because the format of the arguments accepted here differs in a number of ways, and because bp_has_media() 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, 'author_id=4&privacy=public').

  • '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 media objects, 'ids' to get only the media IDs. Default 'all'.
  • 'count_total'
    (string|bool) If true, an additional DB query is run to count the total media items for the query. Default: false.
  • 'sort'
    (string) 'ASC' or 'DESC'. Default: 'DESC'.
  • 'exclude'
    (array|bool) Array of media IDs to exclude. Default: false.
  • 'include'
    (array|bool) Array of exact media 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 media item, this value defaults to the ID of that item. Otherwise the default is false.
  • 'search_terms'
    (string) Limit results by a search term. Default: false.
  • 'user_id'
    (int|array|bool) The ID(s) of user(s) whose media should be fetched. Pass a single ID or an array of IDs. When viewing a user profile page, 'user_id' defaults to the ID of the displayed user. Otherwise the default is false.
  • 'group_id'
    (int|array|bool) The ID(s) of group(s) whose media should be fetched. Pass a single ID or an array of IDs. When viewing a group page, 'group_id' defaults to the ID of the displayed group. Otherwise the default is false.
  • 'privacy'
    (array) Limit results by a privacy. Default: public | grouponly.

Default value: ''

Return

(bool) Returns true when media found, otherwise false.

Source

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

function bp_has_albums( $args = '' ) {
	global $media_album_template;

	/*
	 * Smart Defaults.
	 */

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

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

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

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

	$group_id = false;
	if ( bp_is_group() ) {
		$group_id = bp_get_current_group_id();
		$user_id  = false;
		$privacy = array( 'grouponly' );
	}

	/*
	 * Parse Args.
	 */

	// Note: any params used for filtering can be a single value, or multiple
	// values comma separated.
	$r = bp_parse_args( $args, array(
		'include'           => false,        // Pass an album_id or string of IDs comma-separated.
		'exclude'           => false,        // Pass an activity_id or string of IDs comma-separated.
		'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,

		// Filtering
		'user_id'           => $user_id,     // user_id to filter on.
		'group_id'          => $group_id,    // group_id to filter on.
		'privacy'           => $privacy,     // privacy to filter on - public, onlyme, loggedin, friends, grouponly.

		// Searching.
		'search_terms'      => $search_terms_default,
	), 'has_albums' );

	/*
	 * Smart Overrides.
	 */

	// 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'];
	}

	/*
	 * Query
	 */

	$media_album_template = new BP_Media_Album_Template( $r );

	/**
	 * Filters whether or not there are media albums to display.
	 *
	 * @since BuddyBoss 1.0.0
	 *
	 * @param bool   $value                     Whether or not there are media items to display.
	 * @param string $media_album_template      Current media album template being used.
	 * @param array  $r                         Array of arguments passed into the BP_Media_Album_Template class.
	 */
	return apply_filters( 'bp_has_album', $media_album_template->has_albums(), $media_album_template, $r );
}

Changelog

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