BP_Blogs_Blog::get_all( int|null $limit = null, int|null $page = null )

Retrieve a list of all blogs.

Description

Query will include hidden blogs if the logged-in user has the ‘bp_moderate’ cap.

Parameters

$limit

(Optional) The maximum number of items to return. Default: null (no limit).

Default value: null

$page

(Optional) The page of results to return. Default: null (no limit).

Default value: null

Return

(array) Multidimensional results array, structured as follows: 'blogs' - Array of located blog objects. 'total' - A count of the total blogs.

Source

File: bp-blogs/classes/class-bp-blogs-blog.php

	public static function get_all( $limit = null, $page = null ) {
		global $wpdb;

		$bp = buddypress();

		$hidden_sql = !bp_current_user_can( 'bp_moderate' ) ? "AND wb.public = 1" : '';
		$pag_sql    = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : '';

		$paged_blogs = $wpdb->get_results( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$pag_sql}" );
		$total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql}" );

		// Integer casting.
		foreach ( (array) $paged_blogs as $key => $data ) {
			$paged_blogs[ $key ]->blog_id = (int) $paged_blogs[ $key ]->blog_id;
		}

		return array( 'blogs' => $paged_blogs, 'total' => (int) $total_blogs );
	}

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.