WordPress Snippets at WPcustoms

Count custom post type items in dashboard

Your Dashboard box “at a glance” lists your posts, pages and comments counts. With this function you can add a custom post type count to the list.


/**
 * Snippet Name: Count custom post type items in dashboard
 * Snippet URL: http://www.wpcustoms.net/snippets/count-custom-post-type-items-dashboard/
 */
 function wpc_cpt_glance_counter( $items = array() ) {
    $post_types = array( 'movies' ); // add your custom post type name here
    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, 'your_textdomain' );
            $text = sprintf( $text, number_format_i18n( $published ) );
            if ( current_user_can( $post_type->cap->edit_posts ) ) {
            $output = '' . $text . '';
                echo '
  • ' . $output . '
  • '; } else { $output = '' . $text . ''; echo '
  • ' . $output . '
  • '; } } } return $items; } add_filter( 'dashboard_glance_items', 'wpc_cpt_glance_counter', 10, 1 );