Agregar recuentos de tipos de publicaciones personalizadas al panel de WordPress
El panel de control me muestra convenientemente cuántas publicaciones, páginas y comentarios tengo. Me gustaría que también me mostrara cuántos artículos, videos y caricaturas tengo (tres tipos de publicaciones personalizadas registradas con mi tema). ¿Cómo puedo incluir estos en el panel "Right Now" del dashboard?

Sí, hay varias acciones dentro de ese widget, incluyendo right_now_content_table_end
. Ejemplo:
function my_right_now() {
$num_widgets = wp_count_posts( 'widget' );
$num = number_format_i18n( $num_widgets->publish );
$text = _n( 'Widget', 'Widgets', $num_widgets->publish );
if ( current_user_can( 'edit_pages' ) ) {
$num = "<a href='edit.php?post_type=widget'>$num</a>";
$text = "<a href='edit.php?post_type=widget'>$text</a>";
}
echo '<tr>';
echo '<td class="first b b_pages">' . $num . '</td>';
echo '<td class="t pages">' . $text . '</td>';
echo '</tr>';
}
add_action( 'right_now_content_table_end', 'my_right_now' );

También cubierto en la mejor colección Wiki: http://wordpress.stackexchange.com/questions/1567/best-collection-of-code-for-your-functions-php-file

Basado en este mismo código, puedes usar esto para mostrar el conteo de todos los tipos de publicaciones personalizadas y taxonomías personalizadas:
// Agregar conteos de taxonomías personalizadas y tipos de publicaciones personalizadas al panel
add_action( 'right_now_content_table_end', 'my_add_counts_to_dashboard' );
function my_add_counts_to_dashboard() {
// Conteos de taxonomías personalizadas
$taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' );
foreach ( $taxonomies as $taxonomy ) {
$num_terms = wp_count_terms( $taxonomy->name );
$num = number_format_i18n( $num_terms );
$text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name, $num_terms );
$associated_post_type = $taxonomy->object_type;
if ( current_user_can( 'manage_categories' ) ) {
$num = '<a href="edit-tags.php?taxonomy=' . $taxonomy->name . '&post_type=' . $associated_post_type[0] . '">' . $num . '</a>';
$text = '<a href="edit-tags.php?taxonomy=' . $taxonomy->name . '&post_type=' . $associated_post_type[0] . '">' . $text . '</a>';
}
echo '<td class="first b b-' . $taxonomy->name . 's">' . $num . '</td>';
echo '<td class="t ' . $taxonomy->name . 's">' . $text . '</td>';
echo '</tr><tr>';
}
// Conteos de tipos de publicaciones personalizadas
$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->singular_name, $post_type->labels->name, $num_posts->publish );
if ( current_user_can( 'edit_posts' ) ) {
$num = '<a href="edit.php?post_type=' . $post_type->name . '">' . $num . '</a>';
$text = '<a href="edit.php?post_type=' . $post_type->name . '">' . $text . '</a>';
}
echo '<td class="first b b-' . $post_type->name . 's">' . $num . '</td>';
echo '<td class="t ' . $post_type->name . 's">' . $text . '</td>';
echo '</tr>';
if ( $num_posts->pending > 0 ) {
$num = number_format_i18n( $num_posts->pending );
$text = _n( $post_type->labels->singular_name . ' pendiente', $post_type->labels->name . ' pendientes', $num_posts->pending );
if ( current_user_can( 'edit_posts' ) ) {
$num = '<a href="edit.php?post_status=pending&post_type=' . $post_type->name . '">' . $num . '</a>';
$text = '<a href="edit.php?post_status=pending&post_type=' . $post_type->name . '">' . $text . '</a>';
}
echo '<td class="first b b-' . $post_type->name . 's">' . $num . '</td>';
echo '<td class="t ' . $post_type->name . 's">' . $text . '</td>';
echo '</tr>';
}
}
}

Para cualquiera interesado en cambiar la sección "Discusión" para mostrar el conteo de publicaciones pendientes en lugar de tenerlo todo en "contenido", se puede usar el hook right_now_discussion_table_end de la siguiente manera:
add_action( 'right_now_discussion_table_end', 'my_add_counts_to_rightnow_discussion' );
function my_add_counts_to_rightnow_discussion() {
// Conteo de tipos de publicación personalizados
$post_types = get_post_types( array( '_builtin' => false, 'public' => true , 'show_ui' => true), 'objects' );
foreach ( $post_types as $post_type ) {
if ( current_user_can( 'edit_posts' ) ) {
$num_posts = wp_count_posts( $post_type->name );
$text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num_posts->publish );
$num = number_format_i18n( $num_posts->pending );
$post_types = get_post_types( array( '_builtin' => false, 'public' => true , 'show_ui' => true), 'objects' );
$num = '<a href="edit.php?post_status=pending&post_type=' . $post_type->name . '">' . $num . '</a>';
$text = '<a class="waiting" href="edit.php?post_status=pending&post_type=' . $post_type->name . '">' . $text . ' Pendiente </a>';
echo '<td class="first b b-' . $post_type->name . 's">' . $num . '</td>';
echo '<td class="t ' . $post_type->name . 's">' . $text . '</td>';
echo '</tr>';
}
}
}
También eliminé el if(pending >0)
para que los tipos de publicación se alineen con el conteo de la izquierda. Si usas este código, simplemente usa el código de Sébastien o Adam para los conteos y elimina la sección de pendientes.
También ten en cuenta que agregué una verificación para ver si los tipos de publicación eran públicos y estaban configurados para mostrarse en la interfaz de usuario. Esto se puede agregar al código de cualquiera de los otros.

add_action( 'dashboard_glance_items', 'cor_right_now_content_table_end' );
function cor_right_now_content_table_end() {
$args = array(
'public' => true,
'_builtin' => false
);
$output = 'object';
$operator = 'and';
// Obtener tipos de post personalizados
$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>';
}
// Obtener taxonomías personalizadas
$taxonomies = get_taxonomies( $args, $output, $operator );
foreach ( $taxonomies as $taxonomy ) {
$num_terms = wp_count_terms( $taxonomy->name );
$num = number_format_i18n( $num_terms );
$text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name, intval( $num_terms ) );
if ( current_user_can( 'manage_categories' ) ) {
$output = '<a href="edit-tags.php?taxonomy=' . $taxonomy->name . '">' . $num . ' ' . $text . '</a>';
}
echo '<li class="taxonomy-count ' . $taxonomy->name . '-count">' . $output . '</li>';
}
}
// Añadir CSS personalizado al widget "De un vistazo"
function custom_colors() {
echo '<style type="text/css">
.slides-count a:before {content:"\f233"!important}
.gallery-count a:before {content:"\f163"!important}
</style>';
}
add_action('admin_head', 'custom_colors');
