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
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | 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.