Skip to content

Conversation

@TrivediKavit
Copy link

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.

Add order normalization in the Attachments model initialize method to ensure
the 'order' property is always 'ASC' or 'DESC' (defaulting to 'DESC' for invalid values).
This provides consistent behavior across all attachment collections.

Part of fixing Media Library Grid view ordering when order query var is not normalized.
See #64467.
Since Query inherits from Attachments, and Attachments now normalizes the order
property in its initialize method, remove the duplicate normalization code from
Query.get() to avoid redundancy and ensure consistent behavior.

Part of fixing Media Library Grid view ordering when order query var is not normalized.
See #64467.
@github-actions
Copy link

github-actions bot commented Jan 5, 2026

Hi @TrivediKavit! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

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 trivedikavit, adamsilverstein.

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

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

if ( 'string' === typeof options.props.order ) {
options.props.order = options.props.order.toUpperCase();
if ( 'ASC' !== options.props.order && 'DESC' !== options.props.order ) {
options.props.order = 'DESC';
Copy link
Member

@adamsilverstein adamsilverstein Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this use options.props.order = defaults.order.toUpperCase(); like the original code in query.js that you remove below?

Copy link
Author

@TrivediKavit TrivediKavit Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamsilverstein

We cannot normalize order directly on defaults (e.g., defaults.order.toUpperCase()) in the Attachments model, as defaults refers to Query.defaultProps, which is scoped to the Query class and unavailable in the base Attachments class.

References:

Attempting defaults.order.toUpperCase() in Attachments.initialize() would fail, as defaults is undefined in that scope.

Behavior in Attachments Instances

Attachments is the base Backbone collection for media attachments. On instantiation (e.g., new wp.media.model.Attachments(models, options)), initialize:

  • Sets up this.props with options.props.
  • If options.props.order is a string, normalizes it: uppercases and defaults to 'DESC' if invalid.
  • Ensures consistent order for sorting/filtering, handling case and invalid inputs.

Previously, without normalization in Attachments.initialize(), plain Attachments instances (non-Query subclasses) could use unnormalized order (e.g., 'desc' or 'pizza'), causing inconsistent media library grid sorting.

Impact on Query Instances

Query inherits from Attachments, so normalization is inherited.

  • Before: Normalization in Query.get() ensured valid order for Query instances.
  • After: Normalization in Attachments.initialize() during instantiation (via inheritance). Query.get() applies defaults first, then new Query() triggers normalization.
  • No change in Query behavior; the fix prevents bugs in other Attachments subclasses or direct usage.

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, left one small point of feedback.

@adamsilverstein
Copy link
Member

I tried adding some QUnit tests to validate the order normalization (with help from Claude) in #10682

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