Skip to:
Content
Pages
Categories
Search
Top
Bottom

Excluding certain blogs from bp_has_site_blogs() function

  • @r-a-y

    Keymaster

    Hi BP dev!

    Have a question for you.

    If I wanted to exclude a few blogs from the blog directory, how would I do this?

    Is there an unknown parameter I can add to bp_has_site_blogs()?

    I basically want to remove the home blog and one other one that isn’t really a “blog” per se, b/c they’re more of a content site than a “blog”.

    Thanks for reading,

    -Ray

Viewing 23 replies - 1 through 23 (of 23 total)
  • @r-a-y

    Keymaster

    Friendly courtesy bump.

    @burtadsit

    Participant

    Nope. There’s no hidden ‘exclude’ parameter. If you really need this you can:

    1) Derive a new class from BP_Blogs_Site_Blogs_Template modifying the constructor to exclude the blogs you want from the list of blogs returned by BP_Blogs_Blog::get_<whatever>()

    2) Create a new template tag that modifies the standard version of bp_has_site_blogs() to call your derived template class instead of the standard one.

    3) Modify your theme to use the new bp_has_site_blogs() template loop goodie.

    Like this code here: http://buddypress.pastebin.com/d12aa57a1

    That code is totally untested.

    @burtadsit

    Participant

    My mod to the class constructor is in the spot where the comment // burt! is. Change the array of excluded blogs $my_excluded to reflect the blog ids of the blogs you don’t want to be in the listing.

    @r-a-y

    Keymaster

    Thanks Burt! Much appreciated!

    I’ll give this code a shot and report back.

    @burtadsit

    Participant

    Andy found a problem with the custom class. The totals won’t reflect properly. So you need to modify the code like this one:

    http://buddypress.pastebin.com/m398024c5

    That decrements the blog count total.

    The next problem you’ll have is where to put this. If you put it in bp-custom.php like it is then it will crash and burn because the base class hasn’t been declared yet. So you’ll have to put that in a php file and include it *after* bp loads.

    function bp_loaded(){

    include(‘your-file-with-mods.php’);

    }

    add_action(‘plugins_loaded’,’bp_loaded’);

    If you put that in bp-custom.php it should work right.

    @johnjamesjacoby

    Keymaster

    If a blog is listed as “Private” in the blog settings, that should also exclude it from the list. It will also exclude it from blog search engines and the like. Not sure how private you want this to be, but that’s about as private as it gets. :)

    @r-a-y

    Keymaster

    Burt, I actually did try the first code you mentioned and of course, it did not work in bp-custom.php. I was actually going to post a reply to it, but you already did! Will test out the new code.

    Thanks for your diligence!

    John, one of the blogs in question would be the home blog, so we wouldn’t want to set that to “Private” and disallow search engines ;) But thanks for that little hint, might be useful in the future.

    Cheers guys!

    @r-a-y

    Keymaster

    Burt, the class override seems to work.

    However, one bug I’m getting is an avatar which links to the “Blogs Directory”, as well as a link to “Visit Blog” and the “Latest Post”. Both of which also link to the “Blogs Directory”.

    I also tried the new my_has_site_blogs() function on the “Random Blogs” section and the same thing happens.

    I’ve taken a screenshot so you can see (ignore the ugly styling for now… still a work in progress!):

    http://i41.tinypic.com/10ngnyt.jpg

    Burt, I appreciate the time you’ve put into this matter so far!

    *It might help to say I’m using BP 1.0.1 (not the latest trunk version) and WPMU 2.71.

    @burtadsit

    Participant

    I hooked it up here locally. I see what you mean. Its because we have to reset all the array keys to start at zero and have no gaps. The template loop stuff is funny like that. :)

    $my_excluded = array();
    for ($i = 0; $i < count($this->blogs['blogs'])-1; $i++){
    if (in_array($this->blogs['blogs'][$i]->blog_id, $my_excluded)){
    unset($this->blogs['blogs'][$i]);
    $this->blogs['total'] = ((int)$this->blogs['total'])-1;
    }
    }
    $this->blogs['blogs'] = array_merge( array(), $this->blogs['blogs'] );

    The last bit with the array_merge() does that.

    http://buddypress.pastebin.com/m63458c5c

    @burtadsit

    Participant

    The reset keys thing is courtesy Andy, I remembered it from bp-core-catchuri.php. I just realized what he was doing there and why.

    @r-a-y

    Keymaster

    Hey Burt (and Andy),

    The latest code update fixed the rogue, empty blog listing.

    However, now I’m getting blog id #1 listed all the time, even though I excluded it in the $my_excluded array.

    FYI, I have the following listed in the $my_excluded array:

    $my_excluded = array(1,2);

    Blog id’s 1 and 2 happen to be my only two blogs on my testbox at the moment, so I shouldn’t be seeing any listings under the “Blogs Directory” (I don’t see blog id #2 so that’s good!).

    Two other things I’ve noticed:

    1. The “Random Blogs” section will continue to show either of the excluded blogs depending on the randomness.
    2. Clicking on the letter whose first letter matches the excluded blog in the alphabetical listing will show the excluded blog listing.

    Sorry to have complicated things! Probably a pain to debug!

    @burtadsit

    Participant

    Where are you putting the my_has_site_blogs() fn? That should go in /directories/blogs/blogs-loop.php. Right at the top like:

    <?php if ( my_has_site_blogs( ‘type=active&per_page=10’ ) ) : ?>

    @r-a-y

    Keymaster

    That’s exactly where I have it, Burt.

    @burtadsit

    Participant
    $my_excluded = array('1','15','16','17','18','19','20','21');
    foreach ($this->blogs['blogs'] as $key => $value){
    if (in_array($value->blog_id, $my_excluded)){
    unset($this->blogs['blogs'][$key]);
    $this->blogs['total'] = ((int)$this->blogs['total'])-1;
    }
    }
    $this->blogs['blogs'] = array_merge( array(), $this->blogs['blogs'] );

    The above should fix the issues you were having. Use the foreach() loop instead of the for() loop.

    I tested this and excluded all blogs, some blogs, no blogs. Also with the letter select. The featured blogs loop in /directories/blogs/index.php needs a my_has_site_blogs() also.

    @burtadsit

    Participant

    Remind me not to end a post with the sentence “That code is totally untested”.

    The admin bar Visit > Random Blog is controlled by bp_blogs_redirect_to_random_blog() in bp-blogs.php. You get to play with that one. :)

    @r-a-y

    Keymaster

    Thanks Burt! You’re a savior!

    The code you posted fixed all problems, including the random blogs and alpha listing.

    Case “resolved”! :)

    @burtadsit

    Participant

    Whew.

    @hatiro

    Participant

    Sorry for posting on a resolved topic.

    r-a-y put me on to this post, which is exactly what I need, but reading the thread Burt kindly put some code in the pastebin, which seems to have expired. Any chance it can be posted again as I’ve read through the thread and the penny hasn’t dropped yet…

    Thanks.

    @r-a-y

    Keymaster

    Hey hatiro,

    Here’s the code again on PasteBin:

    http://buddypress.pastebin.com/f36cf5cab

    I’ve set it so it won’t expire.

    Remember to add the bp_loaded() function to your /wp-content/plugins/bp-custom.php

    Also remember to change the blog directory loop in your BP member theme – /wp-content/bp-themes/YOURBPTHEME/directories/blogs/blogs-loop.php

    @hatiro

    Participant

    r-a-y, you are a star, thanks so much for your quick response.

    @amartinezx

    Participant

    Hi there,

    How can I pass the excluded blogs values as an argument:

    Like my_has_site_blogs( 'type=random&max=100&exclude=1,2,3' )

    instead of declaring it.

    $my_excluded = array('1','15','16','17','18','19','20','21');
    foreach ($this->blogs['blogs'] as $key => $value){
    if (in_array($value->blog_id, $my_excluded)){
    unset($this->blogs['blogs'][$key]);
    $this->blogs['total'] = ((int)$this->blogs['total'])-1;
    }
    }
    $this->blogs['blogs'] = array_merge( array(), $this->blogs['blogs'] );

    thank you…

    @blogadera

    Participant

    Is there an updated way to do this 1.2 and above?

    @r-a-y

    Keymaster

    Locking this as this is for BP 1.1 and older.

    Blogadera, please create a new thread and we’ll discuss from there.

Viewing 23 replies - 1 through 23 (of 23 total)
  • The topic ‘Excluding certain blogs from bp_has_site_blogs() function’ is closed to new replies.
Skip to toolbar