List Posts/Pages in WordPress Multisite Admin Sites View

Mulsite admin sites panel showing an extra column indicating the number of published posts and pages on the sites.

This is a little something that’ll help you see activity across a multisite. It adds a column to the admin view that shows the posts/pages count for each site.

I found this plugin that got me the foundation for adding the column and then was able to just use the switch_blog function to get me a count of published posts and pages. Do note that it doesn’t count drafts.1

This column in conjunction with the already existing last updated column should give you a good idea when a site has been abandoned or was never really started.

We could get fancier down the road and do some sort of math to take the last updated date, published posts/pages counts, and any other stuff we want and build a score to rate the likelihood that the site is abandoned.


// Hook to columns on network sites listing
add_filter( 'wpmu_blogs_columns', 'rampages_blogs_columns' );
 
/**
* To add a columns to the sites columns
*
* @param array
*
* @return array
*/
function rampages_blogs_columns($sites_columns)
{
    //array_slice ( array $array , int $offset [, int|null $length = NULL [, bool $preserve_keys = FALSE ]] ) : array

    $columns_1 = array_slice( $sites_columns, 1, 2 );//decide where to put the column in the existing columns
    $columns_2 = array_slice( $sites_columns, 2 );
     
    $sites_columns = $columns_1 + array( 'content' => 'Posts/Pages' ) + $columns_2;//title
     
    return $sites_columns;
}

function sort_rampages_sites_custom_column(){
    $columns['content'] = 'Posts/Pages'; //title it
    return $columns;
}


// Hook to manage column data on network sites listing
add_action( 'manage_sites_custom_column', 'rampages_sites_custom_column', 10, 2 );

/**
* Show page post count
*
* @param string
* @param integer
*
* @return void
*/
function rampages_sites_custom_column($column_name, $blog_id)
{
    if ( $column_name == 'content' ) {
         switch_to_blog($blog_id);//not aware of a function that'll do this based on blog ID so we switch instead
            $pages = wp_count_posts('page','publish')->publish;
            $posts = wp_count_posts('post', 'publish')->publish;
    restore_current_blog();
   
    if ($posts < 1){
        $posts = 0;
    }
    if ($pages < 1){
        $pages = 0;
    }
        echo  $posts . '/' . $pages ;
    }
}




1 I feel like I have more drafts than posts on this site so maybe I need to reconsider that.

Comments on this post

No comments.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL