How to display Custom Post Types in “At A Glance” Meta Box ?

WordPress being the best and most popular CMS framework and due to its flexibility to add new features using a huge library of WordPress plugins, it is now possible to create any type of websites in WordPress. Most of these WordPress plugins and even custom development use WordPress feature of custom post types.

 

 

You might want to show all custom post types used on your website to display in “At a Glance” Metabox of WordPress dashboard. To display custom post types in “At a Glance” meta box add below code in your functions.php file of your active WordPress theme.

Method 1: Simple Filter Hook

				
					add_filter( 'dashboard_glance_items', 'add_cpt_to_glance' );

function add_cpt_to_glance( $items = array() ) {

    $post_types = array( 'your_cpt_slug', 'another_cpt_slug' ); // Add your CPT slugs

    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 ) ) {
                $items[] = sprintf(
                    '<a class="%1$s-count" href="edit.php?post_type=%1$s">%2$s</a>',
                    $type,
                    $text
                );
            } else {
                $items[] = sprintf(
                    '<span class="%1$s-count">%2$s</span>',
                    $type,
                    $text
                );
            }
        }
    }

    return $items;
}
				
			

Method 2: Dynamic — Auto-detect All Public CPTs

				
					add_filter( 'dashboard_glance_items', 'add_all_cpts_to_glance' );

function add_all_cpts_to_glance( $items = array() ) {

    $args = array(
        'public'   => true,
        '_builtin' => false, // Exclude built-in post types (post, page)
    );

    $post_types = get_post_types( $args, 'objects' );

    foreach ( $post_types as $post_type ) {

        $num_posts = wp_count_posts( $post_type->name );
        $published = intval( $num_posts->publish );

        $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 ) ) {
            $items[] = sprintf(
                '<a class="%1$s-count" href="edit.php?post_type=%1$s">%2$s</a>',
                $post_type->name,
                $text
            );
        } else {
            $items[] = sprintf(
                '<span class="%1$s-count">%2$s</span>',
                $post_type->name,
                $text
            );
        }
    }

    return $items;
}
				
			
Share

Disclaimer: *This post may contain affiliate links. I may earn a small commission when you click on the links and buy the products with no additional cost to you. Thank you.

Jetpack

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest
Inline Feedbacks
View all comments