How to Include Custom Post Types in the Right Now Admin Dashboard Widget in WordPress

Home » Snippets » How to Include Custom Post Types in the Right Now Admin Dashboard Widget in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re working with custom post types on your WordPress site, you may want to include them in the "Right Now" dashboard widget so you can see how many of each type you have published and in draft status at a glance. This can be a helpful way to quickly get a sense of the current state of your site. In this article, we’ll show you how to include custom post types in the "Right Now" widget, and customize the display to suit your needs.

					function wpturbo_add_custom_post_types_to_dashboard_widget( $output ) {
    $args = array(
        'public'   => true,
        '_builtin' => false
    );
    $post_types = get_post_types( $args, 'object' );

    $output .= '<ul>';

    foreach ( $post_types as $post_type ) {
        $label = $post_type->label;
        $count = wp_count_posts( $post_type->name )->publish;
        $output .= '<li>' . $label . ' (' . $count . ')</li>';
    }

    $output .= '</ul>';

    return $output;
}
add_filter( 'dashboard_glance_items', 'wpturbo_add_custom_post_types_to_dashboard_widget' );
				

The WordPress Admin Dashboard "Right Now" widget is a handy feature that provides quick information about the site’s content, comments, and user statistics. However, it only includes the information about the default post types registered by WordPress such as posts and pages. That’s why this code snippet will help you to display custom post types in this widget.

In the first part of the code, we define a function called wpturbo_add_custom_post_types_to_dashboard_widget(). This function gets an array of all custom post types that are set to be publicly available. Here, we include _builtin => false in the array to exclude WordPress’s default post types.

$args = array(
    'public'   => true,
    '_builtin' => false
);
$post_types = get_post_types( $args, 'object' );

The next part of the code creates a list of post types with the count of published posts for each post type. We create a variable $output to store our list items, and add each custom post type label and its post count to the list using a foreach loop.

$output .= '<ul>';

foreach ( $post_types as $post_type ) {
    $label = $post_type->label;
    $count = wp_count_posts( $post_type->name )->publish;
    $output .= '<li>' . $label . ' (' . $count . ')</li>';
}

$output .= '</ul>';

To make this information appear in the Right Now widget, we need to hook our function into the dashboard_glance_items filter.

add_filter( 'dashboard_glance_items', 'wpturbo_add_custom_post_types_to_dashboard_widget' );

This will filter the output of the Right Now widget and add our custom post type list to it. Now, whenever the widget loads, it will display all the registered custom post types alongside the default ones, allowing you to check on all of your content at a glance.

Register an account to save your snippets or go Pro to get more features.