Skip to content

Media: Correct attachment count in featured image modal.#11951

Open
SwapnilWordpress wants to merge 2 commits into
WordPress:trunkfrom
SwapnilWordpress:fix/58729-media-library-count
Open

Media: Correct attachment count in featured image modal.#11951
SwapnilWordpress wants to merge 2 commits into
WordPress:trunkfrom
SwapnilWordpress:fix/58729-media-library-count

Conversation

@SwapnilWordpress
Copy link
Copy Markdown

@SwapnilWordpress SwapnilWordpress commented May 22, 2026

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:

  1. Open Posts → edit a post → Set featured image.
  2. Upload a single image.
  3. The footer shows "Showing 1 of 2 media items" instead of "Showing 1 of 1 media items".

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 the X-WP-Total header and local updates on add/remove).

wp.media.model.Attachments updates that total on every observed collection’s add / remove events—not only the mirrored query collection.

For featured image, the library observes:

  • The query collection (uploads and server results)
  • The selection collection (auto-select after upload)

After one upload:

  1. Attachment is added to the query → total +1
  2. autoSelect adds the same attachment to selection → total +1 again

Result: 1 item shown, total reported as 2.

Solution

Only adjust totalAttachments when 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
  • _removeFromTotalAttachments

Demo / 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.

Fixes incorrect media item count shown after uploading and replacing featured images.

See #58729.
@github-actions
Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props swapnil1010.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant