BP_Blogs_Blog::get_blog_extras( array $paged_blogs, array $blog_ids, string|bool $type = false )

Fetch blog data not caught in the main query and append it to results array.

Description

Gets the following information, which is either unavailable at the time of the original query, or is more efficient to look up in one fell swoop:

  • The latest post for each blog, include Featured Image data
  • The blog description

Parameters

$paged_blogs

(array) (Required) Array of results from the original query.

$blog_ids

(array) (Required) Array of IDs returned from the original query.

$type

(string|bool) (Optional) Not currently used. Default: false.

Default value: false

Return

(array) $paged_blogs The located blogs array, with the extras added.

Source

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

	public static function get_blog_extras( &$paged_blogs, &$blog_ids, $type = false ) {
		global $wpdb;

		$bp = buddypress();

		if ( empty( $blog_ids ) )
			return $paged_blogs;

		$blog_ids = implode( ',', wp_parse_id_list( $blog_ids ) );

		for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) {
			$blog_prefix = $wpdb->get_blog_prefix( $paged_blogs[$i]->blog_id );
			$paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT ID, post_content, post_title, post_excerpt, guid FROM {$blog_prefix}posts WHERE post_status = 'publish' AND post_type = 'post' AND id != 1 ORDER BY id DESC LIMIT 1" );
			$images = array();

			// Add URLs to any Featured Image this post might have.
			if ( ! empty( $paged_blogs[$i]->latest_post ) && has_post_thumbnail( $paged_blogs[$i]->latest_post->ID ) ) {

				// Grab 4 sizes of the image. Thumbnail.
				$image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'thumbnail', false );
				if ( ! empty( $image ) )
					$images['thumbnail'] = $image[0];

				// Medium.
				$image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'medium', false );
				if ( ! empty( $image ) )
					$images['medium'] = $image[0];

				// Large.
				$image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'large', false );
				if ( ! empty( $image ) )
					$images['large'] = $image[0];

				// Post thumbnail.
				$image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'post-thumbnail', false );
				if ( ! empty( $image ) )
					$images['post-thumbnail'] = $image[0];

				// Add the images to the latest_post object.
				$paged_blogs[$i]->latest_post->images = $images;
			}
		}

		/* Fetch the blog description for each blog (as it may be empty we can't fetch it in the main query). */
		$blog_descs = $wpdb->get_results( "SELECT blog_id, meta_value as description FROM {$bp->blogs->table_name_blogmeta} WHERE meta_key = 'description' AND blog_id IN ( {$blog_ids} )" );

		for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) {
			foreach ( (array) $blog_descs as $desc ) {
				if ( $desc->blog_id == $paged_blogs[$i]->blog_id )
					$paged_blogs[$i]->description = $desc->description;
			}
		}

		return $paged_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.