Skip to content

Instantly share code, notes, and snippets.

@senlin
Forked from feryardiant/custom_posttype_glance.php
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senlin/af83542a381fb2a7ed67 to your computer and use it in GitHub Desktop.
Save senlin/af83542a381fb2a7ed67 to your computer and use it in GitHub Desktop.
<?php
/**
* Adding Custom post type counts in 'Right now' Dashboard widget.
* Acording this changes :
* - https://core.trac.wordpress.org/ticket/26571
* - https://core.trac.wordpress.org/ticket/26495
* now you can't use 'right_now_*' action API to show your custom post type count from your Dashboard.
* But if you running WP 3.8 or above, you can use 'dashboard_glance_items' instead.
*
* @package Wordpress
* @subpackage Hooks
* @author Fery Wardiyanto <ferywardiyanto@gmail.com>
* @link http://feryardiant.github.com
* @version 1.0
*/
// Add custom post types count action to WP Dashboard
add_action('dashboard_glance_items', 'custom_posttype_glance_items');
// Showing all custom posts count
function custom_posttype_glance_items()
{
$glances = array();
$args = array(
'public' => true, // Showing public post types only
'_builtin' => false // Except the build-in wp post types (page, post, attachments)
);
// Getting your custom post types
$post_types = get_post_types($args, 'object', 'and');
foreach ($post_types as $post_type)
{
// Counting each post
$num_posts = wp_count_posts($post_type->name);
// Number format
$num = number_format_i18n($num_posts->publish);
// Text format
$text = _n($post_type->labels->singular_name, $post_type->labels->name, intval($num_posts->publish));
// If use capable to edit the post type
if (current_user_can('edit_posts'))
{
// Show with link
$glance = '<a class="'.$post_type->name.'-count" href="'.admin_url('edit.php?post_type='.$post_type->name).'">'.$num.' '.$text.'</a>';
}
else
{
// Show without link
$glance = '<span class="'.$post_type->name.'-count">'.$num.' '.$text.'</span>';
}
// Save in array
$glances[] = $glance;
}
// return them
return $glances;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment