bp_has_blogs( array|string $args = '' )

Initialize the blogs loop.

Description

Based on the $args passed, bp_has_blogs() populates the $blogs_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 blogs loop. Most arguments are in the same format as BP_Blogs_Blog::get(). However, because the format of the arguments accepted here differs in a number of ways, and because bp_has_blogs() 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 query string (eg, 'user_id=4&per_page=3').

  • '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) The string used as a query parameter in pagination links. Default: 'bpage'.
  • 'max'
    (int|bool) Maximum number of results to return. Default: false (unlimited).
  • 'type'
    (string) The order in which results should be fetched. 'active', 'alphabetical', 'newest', or 'random'.
  • 'include_blog_ids'
    (array) Array of blog IDs to limit results to.
  • 'sort'
    (string) 'ASC' or 'DESC'. Default: 'DESC'.
  • 'search_terms'
    (string) Limit results by a search term. Default: the value of $_REQUEST['s'] or $_REQUEST['sites_search'], if present.
  • 'user_id'
    (int) The ID of the user whose blogs should be retrieved. When viewing a user profile page, 'user_id' defaults to the ID of the displayed user. Otherwise the default is false.

Default value: ''

Return

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

Source

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

function bp_has_blogs( $args = '' ) {
	global $blogs_template;

	// Check for and use search terms.
	$search_terms_default = false;
	$search_query_arg = bp_core_get_component_search_query_arg( 'blogs' );
	if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
		$search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
	} elseif ( ! empty( $_REQUEST['s'] ) ) {
		$search_terms_default = stripslashes( $_REQUEST['s'] );
	}

	// Parse arguments.
	$r = bp_parse_args( $args, array(
		'type'              => 'active',
		'page_arg'          => 'bpage', // See https://buddypress.trac.wordpress.org/ticket/3679.
		'page'              => 1,
		'per_page'          => 20,
		'max'               => false,
		'user_id'           => bp_displayed_user_id(), // Pass a user_id to limit to only blogs this user is a member of.
		'include_blog_ids'  => false,
		'search_terms'      => $search_terms_default,
		'update_meta_cache' => true
	), 'has_blogs' );

	// Set per_page to maximum if max is enforced.
	if ( ! empty( $r['max'] ) && ( (int) $r['per_page'] > (int) $r['max'] ) ) {
		$r['per_page'] = (int) $r['max'];
	}

	// Get the blogs.
	$blogs_template = new BP_Blogs_Template( $r['type'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['search_terms'], $r['page_arg'], $r['update_meta_cache'], $r['include_blog_ids'] );

	/**
	 * Filters whether or not there are blogs to list.
	 *
	 * @since BuddyPress 1.1.0
	 *
	 * @param bool              $value          Whether or not there are blogs to list.
	 * @param BP_Blogs_Template $blogs_template Current blogs template object.
	 * @param array             $r              Parsed arguments used in blogs template query.
	 */
	return apply_filters( 'bp_has_blogs', $blogs_template->has_blogs(), $blogs_template, $r );
}

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.