How to Show Custom Post Types on WordPress Dashboard

When you log in to your WordPress dashboard, there you’ll see ‘Activities’ or ‘Page Views’ or numbers of published posts things like that.

These are the defaults by WordPress or something added from your installed plugins.

So if you want to show the number of posts you have under the “Custom Post Type” you have made, you’ll just have to add this code to your functions.php or if you are building a plugin, just add this code to your plugin-name.php.


add_filter( 'dashboard_glance_items', 'custom_glance_items', 10, 1 );
function custom_glance_items( $items = array() ) {
    $post_types = array( 'portfolio' );
    foreach( $post_types as $type ) {
        if( ! post_type_exists( $type ) ) continue;
        $num_posts = wp_count_posts( $type );
        if( $num_posts ) {
            $published = intval( $num_posts->publish );
            $post_type = get_post_type_object( $type );
            $text = _n( '%s ' . $post_type->labels->singular_name, '%s ' . $post_type->labels->name, $published, 'lestertheme' );
            $text = sprintf( $text, number_format_i18n( $published ) );
            if ( current_user_can( $post_type->cap->edit_posts ) ) {
            $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $text . '</a>';
                echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
            } else {
            $output = '<span>' . $text . '</span>';
                echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
            }
        }
    }
    return $items;
}

Leave a Reply

Your email address will not be published. Required fields are marked *