Repository navigation
Media: Redirect to the last page when the list view's requested page no longer exists - #16450
rudrakshi-gupta wants to merge 1 commit into
Conversation
…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.
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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. |
Test ReportEnvironment: wordpress-develop Docker, PHP 8.3.35, trunk 9fe0c05 + this PR (merges cleanly), "AI Provider for OpenAI" plugin active so
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; Notes:
The fix works for both reported problems 👍 |
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 whentotal_pages > 0.WP_Query::set_found_posts()returns early when the requested page has no posts, sofound_postsandmax_num_pagesstay0.WP_Media_List_Table::prepare_items()passes those0s straight toset_pagination_args(), so the redirect never runs.WP_Posts_List_Table::prepare_items()has a fallback for this (it recounts withwp_count_posts()whenfound_postsis0past 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 fromwp_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 is0and 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 whileprepare_items()runs, so the redirect keeps the notice arguments and the notice is shown on the last page.edit.phpalready prepares the list before removing them, so this matches the posts screen.Grid mode isn't affected: it loads items over Ajax.
before.mov
after.mov
Testing instructions
wp-admin/upload.php?mode=list&paged=99. Before: empty list. After: redirected to the last page.&attachment-filter=post_mime_type:image,&attachment-filter=detachedand&s=<part of a file name>. You should land on the last page of the filtered 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 argumentsprepare_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.phppasses, and PHPCS is clean on the changed files.Trac ticket: https://core.trac.wordpress.org/ticket/34467
Use of AI Tools
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.