• Skip to primary navigation
  • Skip to content

Gamajo

WordPress Engineer, Code Consultant

  • 🏠️
  • About
  • Services
  • WordPress
  • Articles
  • Contact
  • Show Search
Hide Search

Post Type Counts – How to Add Them to the WordPress Dashboard

Gary Jones · Dec 26, 2013 ·

When a plugin adds one or more custom post types (CPTs), a nice little addition is to give post type counts of the entries on the dashboard, as a summary, where the number of posts and pages is already listed. Doing it for one CPT is easy enough; doing it for multiple CPTs is an obvious case for re-using code, and doing it for CPTs across multiple plugins makes a case for a separate library. Enter Gamajo Dashboard Glancer.

Show Post Type Counts With The Gamajo Dashboard Glancer Library

The Gamajo Dashboard Glancer project centres around a single class, Gamajo_Dashboard_Glancer, which can be included within your CPT plugin. You then instantiate this class, and register post types to it, when you want to show the counts.

<?php
// Require the new class (change to your correct path)
if ( ! class_exists( 'Gamajo_Dashboard_Glancer' ) ) {
require plugin_dir_path( 'includes/class-gamajo-dashboard-glancer.php' );
}
// Hook into the widget (or any hook before it!) to register your items.
add_action( 'dashboard_glance_items', 'prefix_add_dashboard_counts' );
function prefix_add_dashboard_counts() {
$glancer = new Gamajo_Dashboard_Glancer;
$glancer->add( 'my_cpt' ); // show only published "my-cpt" entries
}
view raw my-plugin1.php hosted with ❤ by GitHub

See the project README for more code examples.

The class supports adding multiple CPTs at once, along with multiple statuses, including custom statuses, so there’s a lot of flexibility. The output becomes a list item, and will be linked, if the current user has the capability to edit entries.

The project also includes a second class that extends the first, to provide the appropriate markup for the table-based markup of the Right Now dashboard widget for WordPress 3.7 and earlier.

<?php
// Require the new class (change to your correct path)
if ( ! class_exists( 'Gamajo_Dashboard_Glancer' ) ) {
require plugin_dir_path( 'includes/class-gamajo-dashboard-glancer.php' );
require plugin_dir_path( 'includes/class-gamajo-dashboard-rightnow.php' );
}
// WP 3.8 and later
add_action( 'dashboard_glance_items', 'prefix_add_dashboard_counts' );
function prefix_add_dashboard_counts() {
$post_types = array( 'mp_ingredient', 'mp_recipe', 'mp_mealplan' );
$statuses = array( 'publish', 'draft' );
$glancer = new Gamajo_Dashboard_Glancer;
$glancer->add( $post_types, $statuses );
}
// WP 3.7 and earlier
add_action( 'right_now_content_table_end', 'prefix_add_dashboard_counts_old' );
function prefix_add_dashboard_counts_old() {
$post_types = array( 'mp_ingredient', 'mp_recipe', 'mp_mealplan' );
$statuses = array( 'publish', 'draft' );
$glancer = new Gamajo_Dashboard_RightNow;
$glancer->add( $post_types, $statuses );
}
view raw my-plugin2.php hosted with ❤ by GitHub

With a small bit of styling (not included within the class), you can make your CPT icons appear next to the counts as well:

Screenshot of the At a Glance widget, showing several extra custom post types registered with the post type counts

Get Gamajo Dashboard Glancer class on GitHub.

WordPress

About Gary Jones

Gary Jones is a UK-based WordPress Engineer, code consultant, husband, and father of awesome twins. Driven by a passion for excellence, he develops elegant custom WordPress solutions for marketing departments to maximise their efficiency through his agency Gamajo, and provides services, including code audits, for other designers and developers.

Gary has contributed to most major branches of WordPress Core since 3.3, and contributed to many open source projects in the community. He is a General Translation Editor for the British English locale, has attended many WordCamps, is a three-time organiser of WordCamp London, and a co-organiser and podcast host for WordPress London.

Gary is a key contributor to the Genesis Framework and is a co-host on the UK Genesis podcast.

A former teacher in schools and prisons, Gary's motto is knowledge is power.

Copyright © 2019 · Monochrome Pro on Genesis Framework · WordPress · Log in