Skip to main content
Proper use of switch_to_blog: http://wordpress.stackexchange.com/q/89113/12615
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264
add_action( 'wp_network_dashboard_setup', 'wpse_66963_network_dashboard_setup' );

function wpse_66963_network_dashboard_setup() 
{
    wp_add_dashboard_widget( 
        'wpse_66963_posts_count_widget', 
        '<div id="icon-edit" class="icon32"></div><h2>Network Posts Count</h2>', 
        'wpse_66963_posts_count' );
}

function wpse_66963_posts_count() 
{
    global $wpdb;
    $blogs = $wpdb->get_results( $wpdb->prepare( 
            "SELECT * FROM {$wpdb->blogs} WHERE  spam = '0' 
            AND deleted = '0' AND archived = '0' 
            ORDER BY registered DESC, 5", ARRAY_A ) );
    
    $original_blog_id = get_current_blog_id();

    if ( empty( $blogs ) ) 
    {
        echo '<p>No blogs!</p>';
        break;
    }
    ?>          
    <table class="widefat">
        <thead>
            <tr>
                <th>Site</th>
                <th>Total posts</th>       
                <th>Draft posts</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
            <th>Site</th>
            <th>Total posts</th>
            <th>Draft posts</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
    $args = array(
        'numberposts'     => -1,
        'post_type'       => 'post',
        'post_status'     => 'publish' );
    $total_network = $draft_network = 0;

    foreach ($blogs as $blog) 
    {
        switch_to_blog( $blog->blog_id );

        $args['post_status'] = 'publish';
        $total_posts = count( get_posts( $args ) );
        $total_network += $total_posts;

        $args['post_status'] = 'draft';
        $draft_posts = count( get_posts( $args ) );
        $draft_network += $draft_posts;
        ?>
           <tr>
             <td><?php echo $blog->domain; ?></td>
             <td><?php echo $total_posts; ?></td>
             <td><?php echo $draft_posts; ?></td>
           </tr>
        <?php
    }
    ?>
           <tr>
             <td><b>Total count</b></td>
             <td><?php echo $total_network; ?></td>
             <td><?php echo $draft_network; ?></td>
           </tr>
        </tbody>
    </table>
    <?php
    restore_current_blogswitch_to_blog( $original_blog_id );
}
add_action( 'wp_network_dashboard_setup', 'wpse_66963_network_dashboard_setup' );

function wpse_66963_network_dashboard_setup() 
{
    wp_add_dashboard_widget( 
        'wpse_66963_posts_count_widget', 
        '<div id="icon-edit" class="icon32"></div><h2>Network Posts Count</h2>', 
        'wpse_66963_posts_count' );
}

function wpse_66963_posts_count() 
{
    global $wpdb;
    $blogs = $wpdb->get_results( $wpdb->prepare( 
            "SELECT * FROM {$wpdb->blogs} WHERE  spam = '0' 
            AND deleted = '0' AND archived = '0' 
            ORDER BY registered DESC, 5", ARRAY_A ) );
    
    if ( empty( $blogs ) ) 
    {
        echo '<p>No blogs!</p>';
        break;
    }
    ?>          
    <table class="widefat">
        <thead>
            <tr>
                <th>Site</th>
                <th>Total posts</th>       
                <th>Draft posts</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
            <th>Site</th>
            <th>Total posts</th>
            <th>Draft posts</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
    $args = array(
        'numberposts'     => -1,
        'post_type'       => 'post',
        'post_status'     => 'publish' );
    $total_network = $draft_network = 0;

    foreach ($blogs as $blog) 
    {
        switch_to_blog( $blog->blog_id );

        $args['post_status'] = 'publish';
        $total_posts = count( get_posts( $args ) );
        $total_network += $total_posts;

        $args['post_status'] = 'draft';
        $draft_posts = count( get_posts( $args ) );
        $draft_network += $draft_posts;
        ?>
           <tr>
             <td><?php echo $blog->domain; ?></td>
             <td><?php echo $total_posts; ?></td>
             <td><?php echo $draft_posts; ?></td>
           </tr>
        <?php
    }
    ?>
           <tr>
             <td><b>Total count</b></td>
             <td><?php echo $total_network; ?></td>
             <td><?php echo $draft_network; ?></td>
           </tr>
        </tbody>
    </table>
    <?php
    restore_current_blog();
}
add_action( 'wp_network_dashboard_setup', 'wpse_66963_network_dashboard_setup' );

function wpse_66963_network_dashboard_setup() 
{
    wp_add_dashboard_widget( 
        'wpse_66963_posts_count_widget', 
        '<div id="icon-edit" class="icon32"></div><h2>Network Posts Count</h2>', 
        'wpse_66963_posts_count' );
}

function wpse_66963_posts_count() 
{
    global $wpdb;
    $blogs = $wpdb->get_results( $wpdb->prepare( 
            "SELECT * FROM {$wpdb->blogs} WHERE  spam = '0' 
            AND deleted = '0' AND archived = '0' 
            ORDER BY registered DESC, 5", ARRAY_A ) );
    
    $original_blog_id = get_current_blog_id();

    if ( empty( $blogs ) ) 
    {
        echo '<p>No blogs!</p>';
        break;
    }
    ?>          
    <table class="widefat">
        <thead>
            <tr>
                <th>Site</th>
                <th>Total posts</th>       
                <th>Draft posts</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
            <th>Site</th>
            <th>Total posts</th>
            <th>Draft posts</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
    $args = array(
        'numberposts'     => -1,
        'post_type'       => 'post',
        'post_status'     => 'publish' );
    $total_network = $draft_network = 0;

    foreach ($blogs as $blog) 
    {
        switch_to_blog( $blog->blog_id );

        $args['post_status'] = 'publish';
        $total_posts = count( get_posts( $args ) );
        $total_network += $total_posts;

        $args['post_status'] = 'draft';
        $draft_posts = count( get_posts( $args ) );
        $draft_network += $draft_posts;
        ?>
           <tr>
             <td><?php echo $blog->domain; ?></td>
             <td><?php echo $total_posts; ?></td>
             <td><?php echo $draft_posts; ?></td>
           </tr>
        <?php
    }
    ?>
           <tr>
             <td><b>Total count</b></td>
             <td><?php echo $total_network; ?></td>
             <td><?php echo $draft_network; ?></td>
           </tr>
        </tbody>
    </table>
    <?php
    switch_to_blog( $original_blog_id );
}
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

In form of a Network Dashboard Widget:

network dashboard widget

That <h2> for the widget title is a bit overkill, but was just testing some styling :)

add_action( 'wp_network_dashboard_setup', 'wpse_66963_network_dashboard_setup' );

function wpse_66963_network_dashboard_setup() 
{
    wp_add_dashboard_widget( 
        'wpse_66963_posts_count_widget', 
        '<div id="icon-edit" class="icon32"></div><h2>Network Posts Count</h2>', 
        'wpse_66963_posts_count' );
}

function wpse_66963_posts_count() 
{
    global $wpdb;
    $blogs = $wpdb->get_results( $wpdb->prepare( 
            "SELECT * FROM {$wpdb->blogs} WHERE  spam = '0' 
            AND deleted = '0' AND archived = '0' 
            ORDER BY registered DESC, 5", ARRAY_A ) );
    
    if ( empty( $blogs ) ) 
    {
        echo '<p>No blogs!</p>';
        break;
    }
    ?>          
    <table class="widefat">
        <thead>
            <tr>
                <th>Site</th>
                <th>Total posts</th>       
                <th>Draft posts</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
            <th>Site</th>
            <th>Total posts</th>
            <th>Draft posts</th>
            </tr>
        </tfoot>
        <tbody>
        <?php
    $args = array(
        'numberposts'     => -1,
        'post_type'       => 'post',
        'post_status'     => 'publish' );
    $total_network = $draft_network = 0;

    foreach ($blogs as $blog) 
    {
        switch_to_blog( $blog->blog_id );

        $args['post_status'] = 'publish';
        $total_posts = count( get_posts( $args ) );
        $total_network += $total_posts;

        $args['post_status'] = 'draft';
        $draft_posts = count( get_posts( $args ) );
        $draft_network += $draft_posts;
        ?>
           <tr>
             <td><?php echo $blog->domain; ?></td>
             <td><?php echo $total_posts; ?></td>
             <td><?php echo $draft_posts; ?></td>
           </tr>
        <?php
    }
    ?>
           <tr>
             <td><b>Total count</b></td>
             <td><?php echo $total_network; ?></td>
             <td><?php echo $draft_network; ?></td>
           </tr>
        </tbody>
    </table>
    <?php
    restore_current_blog();
}