Customizing the BuddyPress blog loop

I would like to “omit” certain blogs from the regular BuddyPress blog loop. Either by blog ID or blog Name is fine. Has anyone had success doing this?

Related posts

Leave a Reply

2 comments

  1. Currently the bp_has_blogs() loop does not allow an exclude param to remove certain blogs from the listing. By default all public blogs are listed. You can try a simple hack (but the pagination will be off) http://buddypress.org/community/groups/how-to-and-troubleshooting/forum/topic/excluding-certain-blogs-from-bp_has_blogs-function-for-12/

    Or you can filter on bp_has_blogs and loop over the array and unset (+ recount) prior to being displayed in the theme. While not ideal, pagination would be correct.

    pseudo code for a filter: (though untested as i do run ms+bp setup – have a look at the source code file bp-blogs/bp-blogs-templatetags.php and the class BP_Blogs_Template for further info)

    function my_remove_blog_from_loop( $b, $blogs ) {
    
    foreach ( $blogs->the_blogs as $key => $blog ) {
    
        if ( $blog->blog_id == SOME_BLOG_ID_HERE ) {
    
            unset( $blogs->the_blogs[$key] );
    
            $blogs->blog_count = $blogs->blog_count-1;
            $blogs->total_blog_count = $blogs->total_blog_count-1;
            $blogs->pag_num = $blogs->pag_num -1;
        }
    }
    
    /* Renumber the array keys to account for missing items */
    $blogs_new = array_values( $blogs->blogs );
    $blogs->blogs = $blogs_new;
    
    return $blogs;
    }
    add_action('bp_has_blogs','my_remove_blog_from_loop', 10, 2 );
    

    replace SOME_BLOG_ID_HERE which whatever numerical blog_id

  2. Similar method works to exclude members:

    function custom_remove_members( $m, $members ) {
    
      foreach ( $members->members as $key => $member ) {
    
          if ( $member->id == 1 ) {
    
              unset( $members->members[$key] );
    
              $members->member_count = $members->member_count-1;
              $members->total_member_count = $members->total_member_count-1;
              $members->pag_num = $members->pag_num-1;
          }
      }
    
      /* Renumber the array keys to account for missing items */
      $members_new = array_values( $members->members );
      $members->members = $members_new;
    
      return $members;
    
    }
    
    add_action('bp_has_members','custom_remove_members', 10, 2 );