wordpress-develop icon indicating copy to clipboard operation
wordpress-develop copied to clipboard

Fix: Media Library Grid view displays attachments in the wrong order when order query var is not normalized

Open TrivediKavit opened this issue 2 weeks ago • 4 comments

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

Summary

This PR addresses the Media Library Grid view ordering issue when the order query parameter is not normalized (e.g., order=desc instead of order=DESC). It consolidates order normalization logic in the base Attachments model to ensure consistent behavior across all attachment collections.

Changes Made

In attachments.js:

  • Added order normalization in the initialize method: converts options.props.order to uppercase and defaults to 'DESC' if invalid.
  • Changed this.props.set( _.defaults( options.props || {} ) ); to options.props = _.defaults( options.props || {} ); followed by normalization and then this.props.set( options.props ); for better control.

In query.js:

  • Removed duplicate order normalization code, as Query inherits from Attachments and will now use the parent's normalization logic.

Why This Change?

  • Fixes Root Cause: Ensures this.props starts normalized in Attachments.initialize(), preventing inconsistencies between Query and Attachments instances.
  • Consistency: All attachment collections now handle the order property uniformly, fixing the grid view ordering bug.
  • DRY Principle: Eliminates code duplication between Attachments and Query models.
  • Robustness: Prevents invalid order values from causing UI sorting issues.
  • Backwards Compatibility: No breaking changes; existing valid 'ASC'/'DESC' values work as before, and invalid values now default to 'DESC'.

Safety and Scope

  • Scope: The changes only affect how order props are normalized internally in wp.media.model.Attachments and its subclass wp.media.model.Query. This doesn't alter public APIs or external behavior.
  • Normalization Is Defensive: We're ensuring order is always 'ASC' or 'DESC' (defaulting to 'DESC'), which aligns with existing expectations in the comparator and filters. Invalid inputs (like 'desc' or 'pizza') now behave predictably instead of causing silent failures.
  • Inheritance: Since Query extends Attachments, all media collections benefit from consistent normalization without duplication.
  • No Breaking Changes: Valid 'ASC'/'DESC' values remain unchanged; only unnormalized inputs are fixed. The Trac ticket confirms this resolves the grid view ordering bug without side effects.
  • Potential Edge Cases: If a plugin or theme manually instantiates these models with custom order values, they might see normalized output, but that's actually an improvement (consistent behavior). The media JS isn't loaded on the frontend by default, so no impact on public-facing sites unless explicitly enqueued.

Testing

  • Verified that attachment sorting works correctly in the media library grid view.
  • Ensured Query instances still normalize order via inheritance.
  • Tested with URLs like /wp-admin/upload.php?mode=grid&orderby=date&order=desc to confirm correct ordering.
  • No adverse effects on other WordPress Core functionality.

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.

TrivediKavit avatar Jan 05 '26 05:01 TrivediKavit