WordPress.org

Make WordPress Core

Opened 13 months ago

Last modified 2 months ago

#45035 new enhancement

allow register_post_type() arg to control whether a CPT's counts are added to the "At a Glance" Dashboard widget

Reported by: pbiron Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch has-unit-tests needs-testing
Focuses: administration Cc:
PR Number:

Description (last modified by pbiron)

It would be nice if there an arg to register_post_type() that controlled whether the CPT's counts were added to the "At a Glance" Dashboard widget, e.g.

<?php
register_post_type( 'mycpt', array( ..., 'at_a_glance' => true, ... ) );

Ideally, I think it should default to the value of show_in_menu, but as current behavior is the equivalent of false I'd be OK with that as the default as well.

Another "nice to have" would be to allow the param to take a string value that would be interpreted as a post_status to show the count for, e.g., show counts for custom post_status = mycustompoststatus instead of publish.

I have code that does all of the above, but it would have to be refactored a little bit to submit as a patch (especially to account for the _builtin post_types that already appear in AAG).

If folks think this would be a good enhancement I'd be glad to work on that refactoring and submit a patch.

Attachments (2)

45035_patch.diff (3.2 KB) - added by gwelser 3 months ago.
45035.diff (4.4 KB) - added by gwelser 2 months ago.
Updated patch with unit tests

Download all attachments as: .zip

Change History (9)

#1 @pbiron
13 months ago

  • Description modified (diff)

#2 follow-up: @swissspidy
13 months ago

Another "nice to have" would be to allow the param to take a string value that would be interpreted as a post_status to show the count for, e.g., show counts for custom post_status = mycustompoststatus instead of publish.

What about two arguments:

  • at_a_glance
  • at_a_glance_count_cb (like update_count_callback or register_meta_box_cb)

This way one can provide a custom callback that returns a count. Otherwise it would fall back to wp_count_posts.

#3 in reply to: ↑ 2 @pbiron
13 months ago

Replying to swissspidy:

Another "nice to have" would be to allow the param to take a string value that would be interpreted as a post_status to show the count for, e.g., show counts for custom post_status = mycustompoststatus instead of publish.

What about two arguments:

  • at_a_glance
  • at_a_glance_count_cb (like update_count_callback or register_meta_box_cb)

This way one can provide a custom callback that returns a count. Otherwise it would fall back to wp_count_posts.

So at_a_glance would be a boolean and at_a_glance_count_cb would be ignored if at_a_glance is false.

That certainly works for me.

#4 @johnbillion
12 months ago

  • Keywords needs-patch added; dev-feedback removed

My Extended CPTs library provides a dashboard_glance argument that adds the post type to the At a Glance dashboard widget. I think a custom callback for the count is unnecessary - if you have a custom requirement such as that then the existing dashboard_glance_items filter can be used.

Last edited 12 months ago by johnbillion (previous) (diff)

@gwelser
3 months ago

#5 @gwelser
3 months ago

  • Keywords has-patch needs-testing added; needs-patch removed

I've been thinking about the At a Glance functionality a bit after a recent project and liked the solution presented here. Here is my attempt at a patch (45035_patch.diff).

  • Added at_a_glance arg to register_post_type defaulting to show_in_menu.
  • Added 'at_a_glance' => false to attachment post type registration.
  • Updated wp_dashboard_right_now to pull post types by the new at_a_glance arg.

#6 @gwelser
2 months ago

  • Keywords needs-unit-tests added; needs-testing removed

@gwelser
2 months ago

Updated patch with unit tests

#7 @gwelser
2 months ago

  • Keywords has-unit-tests needs-testing added; needs-unit-tests removed
Note: See TracTickets for help on using tickets.