bp_has_media( array|string $args = '' )
Initialize the media loop.
Description
Based on the $args passed, bp_has_media() populates the $media_template global, enabling the use of BuddyPress templates and template functions to display a list of media items.
Parameters
- $args
-
(Optional) Arguments for limiting the contents of the media loop. Most arguments are in the same format as BP_Media::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, 'user_id=4&fields=all').
- '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) Media 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. - 'scope'
(string) Use a BuddyPress pre-built filter. - 'friends' retrieves items belonging to the friends of a user. - 'groups' retrieves items belonging to groups to which a user belongs to. defaults to 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. - 'album_id'
(int|array|bool) The ID(s) of album(s) whose media should be fetched. Pass a single ID or an array of IDs. When viewing a single album page, 'album_id' defaults to the ID of the displayed album. 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 single group page, 'group_id' defaults to the ID of the displayed group. Otherwise the default is false. - 'privacy'
(array) Limit results by privacy. Default: public | grouponly.
Default value: ''
- 'page'
Return
(bool) Returns true when media found, otherwise false.
Source
File: bp-media/bp-media-template.php
function bp_has_media( $args = '' ) {
global $media_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( 'media' );
if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
$search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
}
// Album filtering
if ( ! isset( $args['album_id'] ) ) {
$album_id = bp_is_single_album() ? (int) bp_action_variable( 0 ) : false;
} else {
$album_id = $args['album_id'];
}
$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_active( 'groups' ) && bp_is_group() ) {
$privacy = array( 'grouponly' );
$group_id = bp_get_current_group_id();
$user_id = false;
}
/*
* 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 media_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.
'order_by' => false, // Order by. Default: date_created
'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.
'album_id' => $album_id, // album_id to filter on.
'group_id' => $group_id, // group_id to filter on.
'privacy' => $privacy, // privacy to filter on - public, onlyme, loggedin, friends, grouponly, message.
// Searching.
'search_terms' => $search_terms_default,
), 'has_media' );
/*
* 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_template = new BP_Media_Template( $r );
/**
* Filters whether or not there are media items to display.
*
* @since BuddyBoss 1.0.0
*
* @param bool $value Whether or not there are media items to display.
* @param string $media_template Current media template being used.
* @param array $r Array of arguments passed into the BP_Media_Template class.
*/
return apply_filters( 'bp_has_media', $media_template->has_media(), $media_template, $r );
}
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.