Published Posts Column in the Network Sites List
Shows how many published posts each site has in Network Admin > Sites, cached for twelve hours so the list stays fast.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
$wpide_post_type = 'post'; // Post type to count.
add_filter( 'wpmu_blogs_columns', function ( $columns ) {
$columns['wpide_post_count'] = __( 'Posts' );
return $columns;
} );
add_action( 'manage_sites_custom_column', function ( $column, $blog_id ) use ( $wpide_post_type ) {
if ( 'wpide_post_count' !== $column ) {
return;
}
$key = 'wpide_post_count_' . (int) $blog_id . '_' . $wpide_post_type;
$count = get_site_transient( $key );
if ( false === $count ) {
switch_to_blog( $blog_id );
$counts = wp_count_posts( $wpide_post_type );
$count = isset( $counts->publish ) ? (int) $counts->publish : 0;
restore_current_blog();
set_site_transient( $key, $count, 12 * HOUR_IN_SECONDS );
}
echo esc_html( number_format_i18n( (int) $count ) );
}, 10, 2 );
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) use ( $wpide_post_type ) {
if ( $post->post_type === $wpide_post_type && ( 'publish' === $new_status || 'publish' === $old_status ) ) {
delete_site_transient( 'wpide_post_count_' . get_current_blog_id() . '_' . $wpide_post_type );
}
}, 10, 3 );
Notes
Change $wpide_post_type to count pages or a custom post type instead.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.