Media: Correct attachment count in featured image modal.#11951
Media: Correct attachment count in featured image modal.#11951SwapnilWordpress wants to merge 2 commits into
Conversation
Fixes incorrect media item count shown after uploading and replacing featured images. See #58729.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Trac ticket: https://core.trac.wordpress.org/ticket/58729
Summary
Fixes incorrect "Showing X of Y media items" text in the media modal when uploading a featured image (and when using Replace featured image).
Problem
When setting a featured image from the post editor:
The displayed count (
X) is correct, but the total (Y) is inflated. The count can also look wrong when reopening the modal via Replace. Same class of bug as #55345 and #57488.Root cause
The media library keeps a server-side total in
totalAttachments(from theX-WP-Totalheader and local updates on add/remove).wp.media.model.Attachmentsupdates that total on every observed collection’sadd/removeevents—not only the mirrored query collection.For featured image, the library observes:
After one upload:
autoSelectadds the same attachment to selection → total +1 againResult: 1 item shown, total reported as 2.
Solution
Only adjust
totalAttachmentswhen the event comes from the mirrored query collection (collection === this.mirroring), not from selection, uploader queue, or other observed collections.File changed:
src/js/media/models/attachments.js_addToTotalAttachments_removeFromTotalAttachmentsDemo / screen recording
Issue Reproduction Video:
current-flow.mp4
Issue Resolution Video
Issue_Resolution_Video.mp4
Use of AI Tools
AI assistance: Yes
Tool(s): Cursor (Auto)
Used for: Exploring the codebase and drafting this PR description. Final code was edited and reviewed by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request.