Skip Navigation

[Resolved] Add Custom Post Type to Dashboard At a Glance Box

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by scottD-2 9 years, 3 months ago.

Assisted by: Waqas.

Author
Posts
#270134

I am trying to add my custom post types to the At A Glance Menu on my site's WP dashboard. I'd like to see in one quick spot how many of each post type there are, and have a link to the /wp-admin/edit.php?post_type=xxxx page.

My attempt to get this to work was to use the following code, but it did not produce the custom post types in the box as I was expecting. (Code source: hidden link)

add_filter( 'dashboard_glance_items', 'custom_glance_items', 10, 1 );

function custom_glance_items( $items = array() ) {

    $post_types = array( 'post_type_1', 'post_type_2' );
    
    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( '%2$s', $type, $text ) . "\n";
            } else {
                $items[] = sprintf( '%2$s', $type, $text ) . "\n";
            }
        }
    }
    
    return $items;
}

Is there an easy way to add these custom post types to the At A Glance Menu?

#270135

From the code provided, it isn't obvious, but I did change post_type_1, post_type_2 to the slugs in my custom post types.
Should I have used something else?

#270332

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Looks like you need to "echo" items, rather just returning the $items. I will suggest you to take a look at following articles:

- hidden link
- https://wordpress.org/support/topic/dashboard-at-a-glance-custom-post-types

However, there's a 3rd party plugin available also, please have a look at https://wordpress.org/plugins/glance-that/

#270384

Thanks, the code in the first link worked perfectly to display the CPT in the At a Glance box. All of them were formatted with the default Post icon (looks like a push pin/thumbtack), which I wanted to change. It was easy enough to do by echoing CSS in that function. What I used was this:

//Add Custom Post Types To Your At A Glance Dashboard Widget
add_action( 'dashboard_glance_items', 'cpad_at_glance_content_table_end' );
function cpad_at_glance_content_table_end() {
	echo "<style type='text/css'>
		   #dashboard_right_now li.post_type_1-count a:before {
				content: '\\f126';
				margin-left: -1px;
			}
		   #dashboard_right_now li.post_type_2-count a:before {
				content: '\\f468';
				margin-left: -1px;
			}
			#dashboard_right_now li.post_type_3-count a:before {
				content: '\\f127';
				margin-left: -1px;
			}
	</style>";
    $args = array(
        'public' => true,
        '_builtin' => false
    );
    $output = 'object';
    $operator = 'and';
 
    $post_types = get_post_types( $args, $output, $operator );
    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->singular_name, $post_type->labels->name, intval( $num_posts->publish ) );
        if ( current_user_can( 'edit_posts' ) ) {
            $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $num . ' ' . $text . '</a>';
            echo '<li class="post-count ' . $post_type->name . '-count">' . $output . '</li>';
        }
    }
}

For anyone looking for this in the future, change post_type_1, post_type_2, post_type_3 in the css to the actual post type name used in the slug.

You can get the content for each post type by right clicking the CPT icon in your WP admin right side menu and selecting Inspect Element. The CSS for that particular post type will include a content number similar to the above code.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.