RE: hiding a site from the blog list in BuddyPress

Re: https://premium.wpmudev.org/forums/topic/how-do-i-hide-a-site-from-the-blog-list-in-buddypress

Vinod Dalvi provided some code to bluekimberlite that hides blogs from lists in BuddyPress. The code also was instrumental in my issue however there was a very minor problem caused by the code ...

I used the code to remove the 'main blog' (blog ID 1) from all the users' blog lists (as it was redundant) and the code did do that. Here is the minor problem, when you visit a user's page that show a list of the blogs they own it shows one extra count in the 'sites' tab.

Ex: If the user has no blog of their own it shows a count of 'Sites [1]' then in the list 'My Sites' it shows 'Sorry, there were no sites found.'

Or if the user has multiple blogs/sites ...

Ex: If the user has 4 blogs of their own it shows a count of 'Sites [5]' then in the list 'My Sites' it shows 'Viewing site 1 to 4 (of 4 sites)' then showing a list of four blogs.

I hope that I was able to explain the problem clear enough, I will also attach an image to help.

  • Vinod Dalvi

    Hi @netwarden,

    Thank you for your question.

    To resolve this issue use the use the following code in the functions.php file of your child theme or add it in your site using the following plugin.

    http://wordpress.org/plugins/code-snippets/

    add_action('bp_blogs_get_blogs','bpdev_exclude_blogs',20,2);
    function bpdev_exclude_blogs($blogs_details=false,$params){
     //list of users to exclude
     $blogs=array();
    
     $excluded_blogs = array(2,3);//comma separated ids of blogs that you want to exclude
    
     $selected_blogs=$blogs_details['blogs'];
     $count=$blogs_details['total'];
     //find and remove
     $blogs_count=count($selected_blogs );
     for($i=0;$i<$blogs_count;$i++){
         if(in_array($selected_blogs[$i]->blog_id,$excluded_blogs )){
             $count--;
             continue;
    
         }
         //otherwise
         $blogs[]=$selected_blogs[$i];
    
     }
     return array('blogs'=>$blogs,'total'=>$count);
    }
    
    function bp_get_total_custom_blog_count_for_user(){
    	global $current_user;
    	$excluded_blogs = array(2,3);//comma separated ids of blogs that you want to exclude
    
    	$excluded_blogs_count = count($excluded_blogs );
    	$total_blogs = ( bp_blogs_total_blogs_for_user( $current_user->ID ) - $excluded_blogs_count );
    
    	return $total_blogs;
    }
    add_filter( 'bp_get_total_blog_count_for_user', 'bp_get_total_custom_blog_count_for_user' );

    Best Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Hi @netwarden,

    Does the 'add_filter' and bp_get_total_custom_blog_count_for_user function have to go somewhere special within the functions.php file?

    Nothing special you can just add it anywhere.

    I have tested it on my test site and it's working fine.

    Could you please try testing it using the default WordPress theme like Twenty Twelve?

    Hi @Vaughan,

    Could you please also test the above provided code?

    Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.