Make WordPress Core

Opened 7 years ago

Last modified 3 weeks 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's profile pbiron Owned by:
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: tested
Focuses: administration Cc:

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 (4)

45035_patch.diff (3.2 KB) - added by gwelser 6 years ago.
45035.diff (4.4 KB) - added by gwelser 6 years ago.
Updated patch with unit tests
at-a-glance-icon.diff (4.2 KB) - added by pbiron 3 weeks ago.
at_a_glance_testing.png (10.3 KB) - added by manishchopra 3 weeks ago.

Download all attachments as: .zip

Change History (18)

#1 @pbiron
7 years ago

  • Description modified (diff)

#2 follow-up: @swissspidy
7 years 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
7 years 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
7 years 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 7 years ago by johnbillion (previous) (diff)

@gwelser
6 years ago

#5 @gwelser
6 years 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
6 years ago

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

@gwelser
6 years ago

Updated patch with unit tests

#7 @gwelser
6 years ago

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

This ticket was mentioned in Slack in #core-test by sirlouen. View the logs.


4 weeks ago

This ticket was mentioned in PR #10647 on WordPress/wordpress-develop by @SirLouen.


4 weeks ago
#9

Following the patch in 45035.diff by @gwelser, I have refreshed and updated to work.

### Testing Instructions

  1. Add a new CPT
  2. Set the at_a_glance argument to true in that CPT
  3. Create a couple posts in that CPT, some published some not published
  4. Go to the admin dashboard, and check if "At a Glance" show the right number of published posts

#10 @SirLouen
4 weeks ago

  • Milestone changed from Awaiting Review to Future Release

Test Report

Description

✅ This report validates that the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/10647.diff

Environment

  • WordPress: 7.0-alpha-61215-src
  • PHP: 8.2.29
  • Server: nginx/1.29.3
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 143.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • BBB Testing Dolly
    • Gutenberg 22.3.0
    • Test Reports 1.2.1

Testing Instructions

Actual Results

  1. ✅ Issue resolved with patch.

Additional Notes

  • I have refreshed 45035.diff to test the idea.
  • @johnbillion what do you say of the idea that @gwelser provided?

Supplemental Artifacts

https://i.imgur.com/3EcHlzn.png

Last edited 4 weeks ago by SirLouen (previous) (diff)

#11 @SergeyBiryukov
4 weeks ago

  • Milestone changed from Future Release to 7.0

@pbiron commented on PR #10647:


3 weeks ago
#12

The other thing this PR should do is output relevant CSS that uses the menu_icon param to register_post_type() for the icon in the At a Glance item. Have to think a bit about the best way to do that, especially to for menu_icon being an inline SVG.

#13 @pbiron
3 weeks ago

https://core.trac.wordpress.org/attachment/ticket/45035/at-a-glance-icon.diff uses the menu_icon arg for the icon of the "At a Glance" item.

It has to be applied to what is already in https://github.com/WordPress/wordpress-develop/pull/10647

#14 @manishchopra
3 weeks ago

  • Keywords tested added; has-patch has-unit-tests needs-testing removed

Test Report

Description

This report validates whether the indicated patch works as expected by allowing
developers to explicitly control whether a custom post type appears in the
“At a Glance” dashboard widget using the at_a_glance argument.

Patch tested:
https://patch-diff.githubusercontent.com/raw/WordPress/wordpress-develop/pull/10647.diff

Environment

  • WordPress: 7.0-alpha-20251226.164314
  • PHP: 8.5.0
  • Server: PHP/8.5.0 (Development Server)
  • Database: mysqli (Server: 9.1.0 / Client: mysqlnd 8.5.0)
  • Browser: Chrome 143.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.1

Steps to Reproduce / Verify

  1. Set up a local WordPress core development environment using the wordpress-develop repository and run WordPress from the build/ directory.
  2. Download and apply the patch: https://patch-diff.githubusercontent.com/raw/WordPress/wordpress-develop/pull/10647.diff using git apply.
  3. Rebuild WordPress assets using npm run build.
  4. Register a custom post type with the at_a_glance argument set to true, for example:
register_post_type(
    'mycpt',
    array(
        'label'       => 'My CPT',
        'public'      => true,
        'show_ui'     => true,
        'at_a_glance' => true,
    )
);
  1. Create and publish at least one post of the custom post type.
  2. Navigate to Dashboard → At a Glance.
  3. Verify that the custom post type appears in the widget with the correct count.
  4. Update the custom post type registration by setting at_a_glance to false (or removing the argument), refresh the dashboard, and verify that the custom post type no longer appears.

Actual Results

  1. ✅ With the patch applied, a custom post type registered with at_a_glance => true appears correctly in the “At a Glance” dashboard widget after publishing at least one post of that type.
  2. ✅ Setting at_a_glance => false (or omitting the argument) prevents the custom post type from appearing in the widget.
  3. ⚠️ Admin button icons (Dashicons) were not visible in the local environment, likely due to a Node.js / npm version mismatch during the build process. This did not affect the functionality or validation of the patch itself.

Additional Notes

  • This functionality is introduced via a patch and is not available in core by default. To test the change locally, the patch must be applied manually.
  • Steps to apply the patch in a local WordPress core development environment:
    1. Download the patch: https://patch-diff.githubusercontent.com/raw/WordPress/wordpress-develop/pull/10647.diff
    2. Place the file in the root of the wordpress-develop repository.
    3. Apply the patch using: git apply 10647.diff
    4. Rebuild WordPress assets (recommended): npm run build
    5. Run WordPress from the build/ directory and verify the behavior via Dashboard → At a Glance.

Supplemental Artifacts


Last edited 3 weeks ago by manishchopra (previous) (diff)
Note: See TracTickets for help on using tickets.