I use WordPress Multisite and in the sidebar i have a box that shows a list of blogs the logged in user is a member of.

I'm looking for a way to exclude the mainblog from this list.
The main blog ID is 1

Here is parts of the code I use:

<?php // Gets user-info ?>
<?php global $current_user; get_currentuserinfo(); 
     $user_info = get_userdata(1); 
     $user_id = $current_user->ID; 

<?php // start the loop ?>
<?php $user_blogs = get_blogs_of_user( $user_id ); echo '<div>'; foreach ($user_blogs AS $user_blog) { echo '';?>

<?php // lists user blogs ?>
<?php echo ''.$user_blog->blogname.' '; ?>

Can anyone help me? :)

1 Answer 1


If you drop this line in your code, you will see all the properties of $user_blogs.
echo '<pre>'.print_r($user_blogs,true).'</pre>';

One of them is userblog_id, so you just have to check against it before echoing the blogname.

$user_blogs = get_blogs_of_user( $user_id ); 
if (!$user_blogs) {
    echo 'no blogs';
} else {
    echo '<div><ul>'; 
    foreach ( $user_blogs as $user_blog ) {
        if ( $user_blog->userblog_id != get_current_blog_id() ) { 
            echo '<li>' . $user_blog->blogname . '</li>';
    echo '</ul></div>'; 
  • it does list blogs without id 1, but it also loops blog nr 1. So if I want to wrap a <li>your code</li> around it it leaves an empty list in the start. Shouldnt the id 1 be excluded in the loop code?
    – Demilio
    Commented May 18, 2012 at 9:03
  • You are only gonna print an empty list item if you want to. I extended my example to make this clearer. You could remove the array item where the blog id equal 1 before doing the foreach, but I don't think it's worth the trouble.
    – brasofilo
    Commented May 18, 2012 at 9:49
  • you did it my friend. Missing a ' in the end </li> there, but perfect!! this was really helpful to me :) thanks!
    – Demilio
    Commented May 18, 2012 at 13:55
  • ok, I have another challange. Is it possible to as an "else" if the user has no blogs?. It would be great to ass a text i.e. "You dont have any blogs yet. Click here to create one"
    – Demilio
    Commented May 18, 2012 at 14:47
  • Added to the code, have phun..!
    – brasofilo
    Commented May 18, 2012 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.