BP_Blogs_Blog::get_by_letter( string $letter, int|null $limit = null, int|null $page = null )

Retrieve a list of blogs whose names start with a given letter.

Description

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

Parameters

$letter

(Required) The letter you're looking for.

$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 matching the query.

Source

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

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

		$bp = buddypress();

		$letter_like = '%' . bp_esc_like( $letter ) . '%';
		$letter_sql  = $wpdb->prepare( 'bm.meta_value LIKE %s', $letter_like );

		$hidden_sql = '';
		if ( !bp_current_user_can( 'bp_moderate' ) )
			$hidden_sql = "AND wb.public = 1";

		$pag_sql = '';
		if ( $limit && $page )
			$pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );

		$paged_blogs = $wpdb->get_results( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND {$letter_sql} {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC{$pag_sql}" );
		$total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND {$letter_sql} {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC" );

		// 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.