How do I omit some sites from being visible in the directory

I would like to list the sub sites, but I want to omit some of the sites from being visible in the listings page. How do I do that?

  • Ash

    Hello @Hazrul Azhar Jamari

    Please use the following code:

    function show_all_sites( $excludes = [] ){
    	$sites = wp_get_sites();
    	$html = '<ul>';
    	foreach( $sites as $site ){
    		if( in_array( $site['blog_id'], $excludes ) ) {
    			continue;
    		}
    		$html .= '<li>';
    			switch_to_blog( $site['blog_id'] );
    			$html .= '<a href="'. get_bloginfo( 'url' ) .'" target="_blank">' . get_bloginfo( 'name' ) . '</a>';
    			restore_current_blog();
    		$html .= '</li>';
    	}
    	$html .= '</ul>';
    	return $html;
    }

    Then use the function anywhere you want to show blog list:

    echo show_all_sites( [2, 3] );

    You have to pass the blog IDs into show_all_sites() functions (I have passed 2 and 2 for example). Those blogs will be excluded from the list.

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.