Exclude current blog ID from User blog list

Hi there!

I would like to list the user blogs excluding the current blog ID
In other words, I allow users to create sites from my main site, so they are user of this main site. I want to exclude this main site from the user blogs list.

This is the current code (it works)

$blogs = get_blogs_of_user($current_user->ID);

	if ( !empty($blogs) ) { ?>

			<p><?php _e( 'Sites you are already a member of:' ) ?></p>
			<ul>
				<?php foreach ( $blogs as $blog ) {
					$home_url = get_home_url( $blog->userblog_id );
					echo '<li><a href="' . esc_url( $home_url ) . '">' . $home_url . '</a></li>';
				} ?>
			</ul>
	<?php }

Thank you :slight_smile:

Mark

  • Predrag Dubajic
    • Support

    Hey @Marc,

    Hope you're doing well today :slight_smile:

    Can you try with this code and tell us if that worked for you:

    $blogs = get_blogs_of_user($current_user->ID);
    
    if ( !empty($blogs) ) { ?>
    
    		<p><?php _e( 'Sites you are already a member of:' ) ?></p>
    		<ul>
    			<?php foreach ( $blogs as $blog ) {
    				if(!in_array($blog->id, 1)) {
    					$home_url = get_home_url( $blog->userblog_id );
    					echo '<li><a href="' . esc_url( $home_url ) . '">' . $home_url . '</a></li>';
    				}
    			} ?>
    		</ul>
    <?php }

    Best regards,
    Predrag

  • MarK
    • Flash Drive

    Hi @Pedrag,

    Thank you for your quick reply!

    Unfortunately it doesn't work adding this code.

    I was looking for something like :

    $blog_id = get_current_blog_id();
    	if ($blogs[ $blog_id ]) unset( $blogs[ $blog_id ] );

    Or maybe using exclude()

    Thanks

    Mark

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.