bp_has_document( array|string $args = '' )

Initialize the document loop.

Description

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

Parameters

$args

(array|string) (Optional) Arguments for limiting the contents of the document loop. Most arguments are in the same format as BP_Document::get(). However, because the format of the arguments accepted here differs in a number of ways, and because bp_has_document() 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) Document fields to retrieve. 'all' to fetch entire document objects, 'ids' to get only the document IDs. Default 'all'.
  • 'count_total'
    (string|bool) If true, an additional DB query is run to count the total document items for the query. Default: false.
  • 'sort'
    (string) 'ASC' or 'DESC'. Default: 'DESC'.
  • 'exclude'
    (array|bool) Array of document IDs to exclude. Default: false.
  • 'include'
    (array|bool) Array of exact document 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 document 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 document 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.
  • 'folder_id'
    (int|array|bool) The ID(s) of folder(s) whose document should be fetched. Pass a single ID or an array of IDs. When viewing a single folder page, 'folder_id' defaults to the ID of the displayed folder. Otherwise the default is false.
  • 'group_id'
    (int|array|bool) The ID(s) of group(s) whose document 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: ''

Return

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

Source

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

function bp_has_document( $args = '' ) {
	global $document_template, $bp;

	$args = bp_parse_args( $args );

	/*
	 * Smart Defaults.
	 */

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

	$privacy = false;

	// folder filtering.
	$folder_id = 0;
	if ( ! isset( $args['folder_id'] ) && empty( $args['include'] ) ) {
		$folder_id = bp_is_single_folder() ? (int) bp_action_variable( 0 ) : false;
		if ( bp_is_group_single() && bp_is_group_folders() ) {
			$folder_id = (int) bp_action_variable( 1 );
		}
	} elseif ( ! empty( $args['folder_id'] ) ) {
		$folder_id = $args['folder_id'];
	}

	$group_id = false;

	// The default scope should recognize custom slugs.
	$scope = ( isset( $_REQUEST['scope'] ) ? $_REQUEST['scope'] : 'all' );
	$scope = bp_document_default_scope( $scope );

	/*
	 * 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 document_id or string of IDs comma-separated.
			'exclude'             => false,         // Pass an activity_id or string of IDs comma-separated.
			'sort'                => 'ASC',         // Sort DESC or ASC.
			'order_by'            => false,         // Order by. Default: title.
			'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,         // Scope - pre-built document filters for a user (friends/groups).
			'scope'               => $scope,        // Filtering.
			'user_id'             => false,         // user_id to filter on.
			'folder_id'           => $folder_id,    // folder_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.
			'folder'              => true,          // privacy to filter on - public, onlyme, loggedin, friends, grouponly, message.
			'user_directory'      => true,          // privacy to filter on - public, onlyme, loggedin, friends, grouponly, message.
			'meta_query_document' => false,
			'meta_query_folder'   => false,
			'meta_query'          => false,

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

	/*
	 * Smart Overrides.
	 */

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

	if ( ! empty( $_REQUEST['sort'] ) ) {
		$r['sort'] = $_REQUEST['sort'];
	}

	if ( isset( $_POST['extras'] ) && ! empty( $_POST['extras']['orderby'] ) && ! empty( $_POST['extras']['sort'] ) ) {
		$r['order_by'] = $_POST['extras']['orderby'];
		$r['sort']     = $_POST['extras']['sort'];
	}

	if ( ! empty( $_REQUEST['order_by'] ) ) {
		$r['order_by'] = $_REQUEST['order_by'];
	}

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

	/*
	 * Query
	 */
	$document_template = new BP_Document_Template( $r );

	/**
	 * Filters whether or not there are document items to display.
	 *
	 * @since BuddyBoss 1.4.0
	 *
	 * @param bool   $value               Whether or not there are document items to display.
	 * @param string $document_template      Current document template being used.
	 * @param array  $r                   Array of arguments passed into the BP_Document_Template class.
	 */
	return apply_filters( 'bp_has_document', $document_template->has_document(), $document_template, $r );
}

Changelog

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