BP_Blogs_Blog::get_blogs_for_user( int $user_id, bool $show_hidden = false )

Get all of a user’s blogs, as tracked by BuddyPress.

Description

Note that this is different from the WordPress function get_blogs_of_user(); the current method returns only those blogs that have been recorded by BuddyPress, while the WP function does a true query of a user’s blog capabilities.

Parameters

$user_id

(Optional) ID of the user whose blogs are being queried. Defaults to logged-in user.

$show_hidden

(Optional) Whether to include blogs that are not marked public. Defaults to true when viewing one's own profile.

Default value: false

Return

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

Source

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

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
public static function get_blogs_for_user( $user_id = 0, $show_hidden = false ) {
    global $wpdb;
 
    $bp = buddypress();
 
    if ( !$user_id )
        $user_id = bp_displayed_user_id();
 
    // Show logged in users their hidden blogs.
    if ( !bp_is_my_profile() && !$show_hidden )
        $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) );
    else
        $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) );
 
    $total_blog_count = BP_Blogs_Blog::total_blog_count_for_user( $user_id );
 
    $user_blogs = array();
    foreach ( (array) $blogs as $blog ) {
        $user_blogs[$blog->blog_id] = new stdClass;
        $user_blogs[$blog->blog_id]->id = (int) $blog->id;
        $user_blogs[$blog->blog_id]->blog_id = (int) $blog->blog_id;
        $user_blogs[$blog->blog_id]->siteurl = ( is_ssl() ) ? 'https://' . $blog->domain . $blog->path : 'http://' . $blog->domain . $blog->path;
        $user_blogs[$blog->blog_id]->name = $blog->name;
    }
 
    return array( 'blogs' => $user_blogs, 'count' => $total_blog_count );
}

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.