Skip to content

Media: Redirect to the last page when the list view's requested page no longer exists - #16450

Open
rudrakshi-gupta wants to merge 1 commit into
WordPress:trunkfrom
rudrakshi-gupta:fix/34467-media-list-out-of-range-page
Open

rudrakshi-gupta wants to merge 1 commit into
WordPress:trunkfrom
rudrakshi-gupta:fix/34467-media-list-out-of-range-page

Conversation

@rudrakshi-gupta

Copy link
Copy Markdown

In the Media Library list view, deleting every item on the last page (or opening any page number past the last one, e.g. upload.php?mode=list&paged=99) shows "No media files found.", "0 items" and no pagination, even though earlier pages still have media. The posts list redirects to the last page in the same situation.

Cause

  • WP_List_Table::set_pagination_args() already redirects to the last page when the page number is too high, but only when total_pages > 0.
  • WP_Query::set_found_posts() returns early when the requested page has no posts, so found_posts and max_num_pages stay 0.
  • WP_Media_List_Table::prepare_items() passes those 0s straight to set_pagination_args(), so the redirect never runs. WP_Posts_List_Table::prepare_items() has a fallback for this (it recounts with wp_count_posts() when found_posts is 0 past page 1). The media list table didn't.

Changes

  • WP_Media_List_Table::prepare_items(): when the requested page is past page 1 and no attachments were found, count the matching attachments with a lightweight query (fields => ids, one row, no meta/term cache) built from wp_edit_attachments_query_vars( $_REQUEST ), the same function that builds the main query. That keeps the media type, "Unattached", month and search filters, including file-name search. set_pagination_args() then redirects to the last page as it does for posts. If nothing matches at all, the count is 0 and nothing changes.
  • upload.php: the media notices (deleted, trashed, attached…) are built at the top of the file, which also removes their arguments from $_SERVER['REQUEST_URI'] before the list table is prepared. Without this change, the redirect above dropped the "N media files permanently deleted." notice. The original request URI is now used only while prepare_items() runs, so the redirect keeps the notice arguments and the notice is shown on the last page. edit.php already prepares the list before removing them, so this matches the posts screen.

Grid mode isn't affected: it loads items over Ajax.

Before After
before.mov
after.mov

Testing instructions

  1. Go to Media → Library, switch to the list view, and set Screen Options → Number of items per page to 5.
  2. Make sure there are at least 12 media files (3 pages).
  3. Go to the last page, select all items, choose Bulk actions → Delete permanently and click Apply.
  4. Before: "No media files found.", "0 items" and no pagination. After: you're taken to the new last page (page 2 of 2), with the "2 media files permanently deleted." notice.
  5. Open wp-admin/upload.php?mode=list&paged=99. Before: empty list. After: redirected to the last page.
  6. Repeat step 5 with filters, e.g. &attachment-filter=post_mime_type:image, &attachment-filter=detached and &s=<part of a file name>. You should land on the last page of the filtered results.
  7. Check that a search with no results (&s=no-such-file&paged=2) still shows "No media files found." without a redirect loop, and that pages within range are unchanged.

Unit tests

Added Tests_Admin_wpMediaListTable::test_prepare_items_counts_attachments_past_the_last_page() (@ticket 34467), which checks the pagination arguments prepare_items() passes for a page past the last one: no filter, media type filter, "Unattached" filter, search by file name, a page within range, and a search without results. The four past-the-last-page cases fail without the fix. tests/phpunit/tests/admin/wpMediaListTable.php passes, and PHPCS is clean on the changed files.

Trac ticket: https://core.trac.wordpress.org/ticket/34467

Use of AI Tools

  • I used Claude Code (Claude Opus 5.5) to write the fixes, and draft the descriptions of PR.
  • I review, test and reproduce things myself before and after the fix.
  • I administer the fixes myself as well and check the code that is being written. Final implementation and tests were reviewed and edited by me.
  • The author takes full responsibility for the submitted code.

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…no longer exists.

In the Media Library list view, deleting every item on the last page left the
user on an empty page with "0 items" and no pagination, because `WP_Query` does
not count the matching attachments when the requested page has none. Recount
them with the same query vars, so `WP_List_Table::set_pagination_args()` can
redirect to the last page, as the posts list table does. Keep the notice
arguments in the URL until then, so the notice is still shown after the redirect.

Fixes #34467.
@github-actions

github-actions Bot commented Dec 3, 2026 •

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.

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

Props rudrakshigupta, ravipatel.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

github-actions Bot commented Dec 3, 2026

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.

@rvpatel

rvpatel commented Dec 3, 2026

Copy link
Copy Markdown

Test Report

Environment: wordpress-develop Docker, PHP 8.3.35, trunk 9fe0c05 + this PR (merges cleanly), "AI Provider for OpenAI" plugin active so connectors_ai_openai_api_key is registered. Requests to /wp/v2/settings as an administrator (application password).

Request Before (trunk) After (this PR)
POST fake key rejected, option '' rejected ✅
PATCH fake key ❌ stored unvalidated (sk-fake-…) ✅ rejected, option ''
POST the masked value returned by GET ❌ stored key wiped ('') ✅ stored key restored before validation
PATCH the masked value ❌ stored key overwritten with the mask (••••••••••••••••EFGH) ✅ same as POST

Calling the new sanitize callback directly: the masked value of the stored key → stored key; a new key → kept; another key's mask → left as-is; '' → '' (clearing still works). ✅

Notes:

  1. After the masked value is restored, _wp_connectors_rest_settings_dispatch() still sends the unchanged stored key to the provider on every save. When the provider can't be reached, _wp_connectors_is_ai_api_key_valid() returns null, and true !== null then clears a working key. Could validation be skipped when the submitted value was the mask (i.e. the key didn't change)?
  2. _wp_connectors_sanitize_api_key() is new, so @since 7.1.0 should be 7.3.0.
  3. There are no unit tests yet. PATCH validation and the masked-resubmit case would be good to cover.
  4. The PR has no Trac ticket; one is needed before it can be committed.

The fix works for both reported problems 👍

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.

2 participants