Add Custom Post Types to WordPress’ “At a Glance” Dashboard

Lots of outdated information on this floating around the web, so here’s the latest way you can add your custom post types to the “At a Glance” panel in the WordPress Dashboard. Here’s an example showing the end result:

screenshot showing various custom post types in WordPress, in the Dashboard's At a Glance section, listing the number of posts within each  post type
Various custom post types shown via this code on a WordPress site.

Here’s the code, for your functions.php:

// Add custom taxonomies and custom post types counts to dashboard
add_action( 'dashboard_glance_items', 'cpt_to_at_a_glance' );
function cpt_to_at_a_glance() {
// Custom post types counts
$post_types = get_post_types( array( '_builtin' => false ), 'objects' );
foreach ( $post_types as $post_type ) {
$num_posts = wp_count_posts( $post_type->name );
$num = number_format_i18n( $num_posts->publish );
$text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num_posts->publish );
if ( current_user_can( 'edit_posts' ) ) {
$num = '<li class="post-count"><a href="edit.php?post_type=' . $post_type->name . '">' . $num . ' ' . $text . '</a></li>';
}
echo $num;
}
}

Up Next: Detect if style.min.css Exists in WordPress