When you log into your WordPress dashboard, one of the widgets that you see it the one titled ‘At a Glance’. This WordPress dashboard widget is specifically designed to help users to immediately know the number of posts, pages, and comments published on the website.
Since WordPress has increasingly become the content management system of choice for various projects, it would be useful if we would use ‘at glance’ dashboard widget to showcase more data.
In this post, I want us to focus on how we can custom post types to ‘at a glance’ dashboard widget to enable us to know the number of specific custom posts that have been published just like the native WordPress posts and pages.
This may be an important step when you want to publish a significant amount of content using the custom post types.
You need to add custom post types to ‘at a glance’ dashboard widget so that you can quickly and easily access the content published in each custom post type.
Example of Adding Custom Post Types to ‘At Glance’ Dashboard Widget
In a recent project, a client wanted me to build a content management system based on WordPress for various dog breeds.
Ideally, we were supposed to use custom post types to display the dog breed and advanced WordPress MetaBoxes or advanced custom fields to showcase various characteristics of each dog breed.
The first step was obviously to register custom post types for the dog breed but we also needed to custom post types to ‘at a glance’ dashboard widget so that the administrators and editors would easily now the number of dog breeds that had been published.
If you don’t know how to create custom post types in WordPress, you can check out a recent tutorial that I shared with a step by step approach – how to create custom post types in WordPress without using a plugin.
Add Custom Post Types to ‘At Glance’ WordPress Dashboard Widget
After creating the custom post type we now need to move to the next step and add custom post types to ‘at a glance’ dashboard widget using the following code:
add_action( 'dashboard_glance_items', 'add_dog_breed_cpt_to_at_a_glance' );
First we create an action hook that hooks on the ‘dashboard_glance_items’, event, and with a callback function that will contain the logic of the content we want to display – ‘add_dog_breed_cpt_to_at_a_glance’.
If you are not familiar with how action hooks work, it may be a good idea to read my previous post – WordPress custom hooks.
The callback function contains all the logic that we use to add custom post types to ‘at glance’ dashboard widget:
/** * Show the dog breed custom post type on WordPress Dashboard At a Glance widget */ function add_dog_breed_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->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; } }
You can add this code to the functions.php file or you can include it in your plugin files. After updating the custom post types should be visible on ‘At glance’ WordPress dashboard widget as shown in the image below:
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Wrapping up
As a WordPress plugin developer or theme developer there are several other ways you can enhance WordPress to make it the best choice for every client website or project especially using the custom post types. Dashboard widgets can also be useful in displaying more data from both native and custom posts. I hope this quick post helps you add the custom post types to ‘At a Glance’ dashboard widget on your WordPress site.
Comments are closed.