Skip to:
Content
Pages
Categories
Search
Top
Bottom

Excluding certain blogs from bp_has_site_blogs() function

  • Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    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)
  • Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Friendly courtesy bump.

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    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.

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    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.

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Thanks Burt! Much appreciated!

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

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    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.

    Avatar of John James Jacoby
    John James Jacoby
    Keymaster

    @johnjamesjacoby

    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. :)

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    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!

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    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.

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    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

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    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.

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    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!

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    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′ ) ) : ?>

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    That’s exactly where I have it, Burt.

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    $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.

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    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. :)

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Thanks Burt! You’re a savior!

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

    Case “resolved”! :)

    Avatar of Burt Adsit
    Burt Adsit
    Participant

    @burtadsit

    Whew.

    Avatar of hatiro
    HaTiRo
    Participant

    @hatiro

    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.

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    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

    Avatar of hatiro
    HaTiRo
    Participant

    @hatiro

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

    Avatar of amartinezx
    amartinezx
    Participant

    @amartinezx

    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…

    Avatar of blgdra
    blgdra
    Participant

    @blogadera

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

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    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.