Make WordPress Core

Opened 3 years ago

Last modified 5 months ago

#58729 new defect (bug)

Incorrect Information Displayed in Media Library Window when Uploading Featured Image

Reported by: adhun Owned by:
Priority: normal Milestone: Awaiting Review
Component: Media Version:
Severity: normal Keywords: has-patch
Cc: Focuses: ui, javascript

Description

I encountered an issue while attempting to upload a featured image for a post on WordPress. The problem arises when I access the Media Library window, as it displays incorrect information regarding the number of uploaded items.

Steps to Reproduce:

  1. Log in to the WordPress admin panel.
  2. Navigate to the "Posts" section and select an existing post or create a new one.
  3. Click on the "Set featured image" button within the post editor.
  4. After uploading the first image its showing wrong information of number of media, Instead of Showing 1 of 1 media items its showing Showing 1 of 2 media items
  5. It appears that the number of media items is inconsistent when I attempt to open the media library again by selecting the "replace" option.

Video Link : ​https://www.loom.com/share/f87871494c5145c385f2343b454fe262

Change History (6)

#1 @mukesh27
3 years ago

  • Version trunk

Hi there, thanks for the ticket.

I reproduce same issue in trunk and 6.2.2 so it was old issue. Removing version from ticket.

This ticket was mentioned in ​Slack in #accessibility by joedolson. ​View the logs.


3 years ago

#3 @joedolson
3 years ago

  • Focuses accessibility removed

This ticket doesn't have any accessibility issues outside of the incorrect data that impacts all users equally.

#4 @joedolson
3 years ago

  • Component Administration → Media

This ticket was mentioned in ​PR #11951 on ​WordPress/wordpress-develop by ​@swapnil1010.


5 months ago
#5

  • Keywords has-patch added; needs-patch removed

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:

​https://github.com/user-attachments/assets/20526e3b-f867-49f2-ba4d-46e42a19be10

Issue Resolution Video

​https://github.com/user-attachments/assets/5614f4d9-06ec-4898-9ff4-67a3865b08a7

## Test plan

  • [ ] Featured image (new): Post editor → Set featured image → upload one image → footer shows "Showing 1 of 1 media items".
  • [ ] Featured image (replace): Set an image, then Replace → upload another → count stays consistent.
  • [ ] Media Library: upload.php → upload/browse → load-more text still correct when many items exist.
  • [ ] Gallery block: Add gallery → Media Library → upload several images → displayed vs total counts stay in sync (related to #57488).

## Checklist

  • [ ] Trac ticket linked above
  • [ ] Allow edits and access to secrets by maintainers enabled on this PR

This ticket was mentioned in ​Slack in #core-media by swapnil1010. ​View the logs.


5 months ago

Note: See TracTickets for help on using tickets.