Show Custom Post Types in "At a Glance"
Adds published counts for public custom post types (portfolio items, products, events and so on) to the At a Glance Dashboard widget.
Opens WPIDE on
Code
add_filter( 'dashboard_glance_items', function ( $items ) {
$post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
foreach ( $post_types as $type ) {
$counts = wp_count_posts( $type->name );
$published = isset( $counts->publish ) ? (int) $counts->publish : 0;
$label = number_format_i18n( $published ) . ' ' . ( 1 === $published ? $type->labels->singular_name : $type->labels->name );
if ( current_user_can( $type->cap->edit_posts ) ) {
$items[] = sprintf( '<a class="%1$s-count" href="%2$s">%3$s</a>', esc_attr( $type->name ), esc_url( admin_url( 'edit.php?post_type=' . $type->name ) ), esc_html( $label ) );
} else {
$items[] = sprintf( '<span class="%1$s-count">%2$s</span>', esc_attr( $type->name ), esc_html( $label ) );
}
}
return $items;
} );
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.