Make WordPress Core

Opened 7 years ago

Last modified 3 weeks ago

#45076 new defect (bug)

Category counter is not updated

Reported by: mukesh27's profile mukesh27 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-screenshots dev-feedback has-patch needs-refresh
Focuses: administration Cc:

Description

When we create new category from admin panel category successfully added in right category panel but it's counter is not updated.

Suppose i have create five category like ''Category 1'', ''Category 1.1'', ''Category 2'', ''Category 2.1'', ''Uncategorized'' system show counter as 5 items but when i create new category called ''Category 3'' it will added successfully in category list but total counter still show 5 items it should show 6 items but when i refresh that page it show counter as 6 items.

Check video https://youtu.be/rBmziC5_0XQ

Change History (7)

This ticket was mentioned in Slack in #core by mukeshpanchal27. View the logs.


7 years ago

#2 @strangerstudios
7 years ago

I've confirmed that the item count (for total cats, not posts per cat) does NOT update when adding a new category. It also doesn't update when deleting a category.

Tested in WordPress 5.0

This is because the form submission is handled by AJAX. The AJAX response does not include the count or JS code to update it.

Here is the AJAX handler in PHP:
https://github.com/WordPress/WordPress/blob/5.0-branch/wp-admin/includes/ajax-actions.php#L877-L930

Here is the JS that kicks off and processes that response:
https://github.com/WordPress/WordPress/blob/5.0-branch/wp-admin/js/tags.js#L89-L146

We could update the wp_ajax_add_tag() function in ajax-actions.php to query for the count of categories and return that in the AJAX response. Then update tags.js to process that to update the counts on the screen.

Alternatively, we could just update the JS to increment or decrement the items number when a category is added or removed. I think this would be good enough even if it would be incorrect when multiple admins are updating categories from different screens.

This is also fairly minor and may not be worth fixing.

#3 @sourav08
9 months ago

#63395 was marked as a duplicate.

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


7 months ago
#4

  • Keywords has-patch added; needs-patch removed

#5 @hbhalodia
7 months ago

Hi @mukesh27, @sourav08, @strangerstudios,

In addition to incrementing the category count when adding a term, it is also necessary to decrement the count when deleting a single term to ensure accuracy.

I have observed that for both add and delete operations, we trigger an AJAX call to insert or remove the term from the site. Upon successful completion, the term count should be updated accordingly by incrementing or decrementing by one, utilizing the count value already displayed on the current page.

Please find the pull request implementing this functionality here: [PR](https://github.com/WordPress/wordpress-develop/pull/9336)

That said, there are a couple of points to consider:

  1. When new tags are added, the count is initially hidden. To display it properly, we need to apply styles dynamically via JavaScript to remove the display: none attribute.
  2. When the count changes from singular to plural or vice versa (e.g., from "1 item" to "2 items"), the current logic only appends 's' to form the plural, which does not accommodate localization. Since item/items is a localized string, pluralization varies across languages. Consequently, handling this pluralization programmatically based on the count is not feasible within the current framework. This issue also applies in reverse when decrementing from plural to singular.

Please let me know if you have any suggestions or further insights on handling the localization nuances.

#6 @desrosj
7 months ago

#63395 was marked as a duplicate.

#7 @huzaifaalmesbah
3 weeks ago

  • Keywords needs-refresh added; needs-testing removed

Patch Testing Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/9336

Environment

  • WordPress: 7.0-alpha-61215-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 143.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Two 2.1
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.1

Steps taken

  1. Logged into the WordPress admin dashboard.
  2. Navigated to Posts → Categories.
  3. Observed the category counter displayed at the top of the category list.
  4. Added a new category using the Add New Category form.
  5. Verified that the new category appears in the list.
  6. Noted that the category counter does not update immediately and only updates after refreshing the page (issue reproduction).
  7. Attempted to apply the patch from PR
  8. Patch application failed with rejected hunks in src/js/_enqueues/admin/tags.js.
  9. ❌ Patch is failing

Expected result

  • When a new category is added or an existing category is deleted via the admin panel, the total category counter should update immediately without requiring a page refresh.
  • The patch should apply cleanly and enable this behavior.

Additional Notes

  • The patch appears to be out of sync with the current version of tags.js.
  • A refreshed or rebased patch is required before testing can proceed.
  • Once updated, testing should verify both add and delete category actions.
Last edited 3 weeks ago by huzaifaalmesbah (previous) (diff)
Note: See TracTickets for help on using tickets.