BP_Blogs_Blog::get( string $type, int|bool $limit = false, int|bool $page = false, int $user_id, string|bool $search_terms = false, bool $update_meta_cache = true, array|bool $include_blog_ids = false )
Retrieve a set of blog-user associations.
Description
Parameters
- $type
-
(Required) The order in which results should be returned. 'active', 'alphabetical', 'newest', or 'random'.
- $limit
-
(Optional) The maximum records to return. Default: false.
Default value: false
- $page
-
(Optional) The page of records to return. Default: false (unlimited results).
Default value: false
- $user_id
-
(Optional) ID of the user whose blogs are being retrieved. Default: 0.
- $search_terms
-
(Optional) Search by text stored in blogmeta (such as the blog name). Default: false.
Default value: false
- $update_meta_cache
-
(Optional) Whether to pre-fetch metadata for blogs. Default: true.
Default value: true
- $include_blog_ids
-
(Optional) Array of blog IDs to include.
Default value: false
Return
(array) Multidimensional results array, structured as follows: 'blogs' - Array of located blog objects 'total' - A count of the total blogs matching the filter params
Source
File: bp-blogs/classes/class-bp-blogs-blog.php
public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false, $update_meta_cache = true, $include_blog_ids = false ) { global $wpdb; $bp = buddypress(); if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) ) $hidden_sql = "AND wb.public = 1"; else $hidden_sql = ''; $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; $user_sql = !empty( $user_id ) ? $wpdb->prepare( " AND b.user_id = %d", $user_id ) : ''; switch ( $type ) { case 'active': default: $order_sql = "ORDER BY bm.meta_value DESC"; break; case 'alphabetical': $order_sql = "ORDER BY bm_name.meta_value ASC"; break; case 'newest': $order_sql = "ORDER BY wb.registered DESC"; break; case 'random': $order_sql = "ORDER BY RAND()"; break; } $include_sql = ''; $include_blog_ids = array_filter( wp_parse_id_list( $include_blog_ids ) ); if ( ! empty( $include_blog_ids ) ) { $blog_ids_sql = implode( ',', $include_blog_ids ); $include_sql = " AND b.blog_id IN ({$blog_ids_sql})"; } if ( ! empty( $search_terms ) ) { $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; $search_terms_sql = $wpdb->prepare( 'AND (bm_name.meta_value LIKE %s OR bm_description.meta_value LIKE %s)', $search_terms_like, $search_terms_like ); } else { $search_terms_sql = ''; } $paged_blogs = $wpdb->get_results( " SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm_name.meta_value as name FROM {$bp->blogs->table_name} b LEFT JOIN {$bp->blogs->table_name_blogmeta} bm ON (b.blog_id = bm.blog_id) LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id) LEFT JOIN {$wpdb->users} u ON (b.user_id = u.ID) WHERE wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' {$search_terms_sql} {$user_sql} {$include_sql} GROUP BY b.blog_id {$order_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) LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) WHERE wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' {$search_terms_sql} {$user_sql} {$include_sql} " ); $blog_ids = array(); foreach ( (array) $paged_blogs as $blog ) { $blog_ids[] = (int) $blog->blog_id; } $paged_blogs = BP_Blogs_Blog::get_blog_extras( $paged_blogs, $blog_ids, $type ); // Integer casting. foreach ( (array) $paged_blogs as $key => $data ) { $paged_blogs[ $key ]->blog_id = (int) $paged_blogs[ $key ]->blog_id; $paged_blogs[ $key ]->admin_user_id = (int) $paged_blogs[ $key ]->admin_user_id; } if ( $update_meta_cache ) { bp_blogs_update_meta_cache( $blog_ids ); } return array( 'blogs' => $paged_blogs, 'total' => $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.