6

Can I list all the blogs in my network on one page?

4 Answers 4

7

yes, small source for an template.

<ul class='postlist no-mp'>

<?php 
/**
 * Old version, change on 07/23/2013
 *
$blogs = $wpdb->get_results( 
    "SELECT blog_id,path FROM {$wpdb->blogs} 
    WHERE blog_id != {$wpdb->blogid} 
    AND site_id = '{$wpdb->siteid}' 
    AND spam = '0' 
    AND deleted = '0' 
    AND archived = '0' 
    order by blog_id", ARRAY_A
); 
 */

// get all blogs
$blogs = get_blog_list( 0, 'all' );

if ( 0 < count( $blogs ) ) :
    foreach( $blogs as $blog ) : 
        switch_to_blog( $blog[ 'blog_id' ] );

        if ( get_theme_mod( 'show_in_home', 'on' ) !== 'on' ) {
            continue;
        }

        $description  = get_bloginfo( 'description' );
        $blog_details = get_blog_details( $blog[ 'blog_id' ] );
        ?>
        <li class="no-mp">

            <h2 class="no-mp blog_title">
                <a href="<?php echo $blog_details->path ?>">
                    <?php echo  $blog_details->blogname; ?>
                </a>
            </h2>

            <div class="blog_description">
                <?php echo $description; ?>
            </div>

            <?php 
            query_posts( 'showposts=5' );
            if ( have_posts() ) :
                while( have_posts() ) :
                    the_post();
                    ?>
                    <div class="blog_post">
                        <div class="post_title">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </div>
                        <div class="post_excerpt">
                            <?php the_excerpt(); ?>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php endif; 
            restore_current_blog();
            ?>
        </li>
<?php endforeach;
endif; ?>
</ul>
3
  • this one didnt work. Got an error on the endforeach;
    – Demilio
    Feb 11, 2012 at 19:38
  • 1
    sorry, i have forgotten an endif, last line; now it works; have tested now.
    – bueltge
    Feb 11, 2012 at 19:51
  • Small hint to use it without a db select. Current in my projects I use this snippet: $blogs = get_blog_list( 0, 'all' ); . This get a array of data about all blogs inside the MU network. Is easier to maintenance. All data to each blog can get the the follow snipper get_blog_details( $blog['blog_id'] ). Maybe this helps as note to my answer.
    – bueltge
    Jul 23, 2013 at 6:28
4

This will print out an unordered list of all public sites in a multisite network:

// $bcount = get_blog_count();
global $wpdb;
$blogs = $wpdb->get_results("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'");
if(!empty($blogs)){
    ?><ul class="menu"><?php
    foreach($blogs as $blog){
        $details = get_blog_details($blog->blog_id);
        if($details != false){
            $addr = $details->siteurl;
            $name = $details->blogname;
            if(!(($blog->blog_id == 1)&&($show_main != 1))){
                ?>
                <li class="menu-item<?php if($counter == get_current_blog_id()){ echo ' current-menu-item';}?>">
                    <a href="<?php echo $addr; ?>"><?php echo $name;?></a>
                </li>
                <?php
            }
        }
    }
    ?></ul><?php
}
6
  • ahh, thank you!! is it possible to show these alphabeticly?
    – Demilio
    Feb 9, 2012 at 18:16
  • Instead of echoing them out, store them in an array using the blogs name as the key, do an array sort, then echo out each item in the array
    – Tom J Nowell
    Feb 10, 2012 at 17:40
  • Why do you need first line ($bcount = get_blog_count();)? This variable is not used anywhere.
    – trejder
    Nov 9, 2012 at 21:18
  • Indeed it isn't, perhaps that line should be removed
    – Tom J Nowell
    Nov 10, 2012 at 0:25
  • Hi! Tom J, Hi! Your code works well for me. But, I have a question. I want to exclude certain site from appearing from this list. How can it be done? Thank you!
    – Jornes
    Apr 28, 2020 at 14:37
0

This is a solution written basing on Tom J Nowell idea and answer. It prints out sorted list of all sites in Wordpress Multisite installation, as simple line (separated with pipe).

To get this solution running, edit your currently selected theme and select shortcodes.php from right sidebar. Near the end of this file, before first occurence of add_shortcode call add following function:

function theme_list_all_network_sites()
{
    global $wpdb;

    $result = '';
    $sites = array();
    $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'"));

    if(!empty($blogs))
    {
        foreach($blogs as $blog)
        {
            $details = get_blog_details($blog->blog_id);

            if($details != false)
            {
                $url = $details->siteurl;
                $name = $details->blogname;

                if(!(($blog->blog_id == 1) && ($show_main != 1)))
                {
                    $sites[$name] = $url;
                }
            }
        }

        ksort($sites);

        $count = count($sites);
        $current = 1;

        foreach($sites as $name=>$url)
        {
            $result.= '<a href="'.$url.'">'.$name.'</a>';
            $result.= ($current == $count) ? "\n" : ' | ';

            ++$current;
        }
    }

    return $result;
}

Then scroll down to the end of file and after last occurence of add_shortcode add:

add_shortcode('network_list', 'theme_list_all_network_sites');

Click Update File to save your changes.

Now, whenever anyone use [network_list] shortcode to in post, page or theme element, a list of network sites will be printed in place of that shortcode.

0

Since WordPress 4.6.0, I offer you a more modern way to "list" (or more) :

<?php $sites_q = new \WP_Site_Query( [
    'site__not_in' => get_main_site_for_network(),
] );
$sites_for_network = $sites_q->get_sites();
if ( ! empty( $sites_for_network ) ) {
    $original_site_id = get_current_blog_id();
    foreach ( $sites_for_network as $site ) {
         /** @var $site \WP_Site */
         switch_to_blog( $site->blog_id );
         // Do stuff with the site
    }
    switch_to_blog( $original_site_id );
}

Your Answer

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

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