-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add menu_order sorting option to Query Loop block #68781
base: trunk
Are you sure you want to change the base?
Conversation
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@@ -75,6 +75,37 @@ export function QueryControls( { | |||
// but instead are destructured inline where necessary. | |||
...props | |||
}: QueryControlsProps ) { | |||
const orderByList = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name here matches the props.categoriesList
convention used below with orderBy
being the feature.
@@ -23,6 +23,16 @@ const orderOptions = [ | |||
label: __( 'Z → A' ), | |||
value: 'title/desc', | |||
}, | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These options should be available only to post types that support page attributes. Where and how do we add that logic? Here is a reference to how the "order" setting is decided at the page settings three-dots:
gutenberg/packages/editor/src/dataviews/store/private-actions.ts
Lines 156 to 158 in be5ce8a
postTypeConfig.supports?.[ 'page-attributes' ] | |
? reorderPage | |
: undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if it helps but there is a PageAttributesCheck
, that you can take inspiration from:
https://github.com/WordPress/gutenberg/blob/trunk/packages/editor/src/components/page-attributes/check.js
A check for the post type support should probably go in the block's utils.js file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is the only remaining task before this can be merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed! We need a decision on which post context information will be passed to this component in order for it to limit the available sorting options to what is supported by the post type (include the menu_order only if the post type supports that).
Based on my current understanding, I would recommend passing the postType data getPostType()
. This would follow the existing convention of selected categories being passed to the component instead of the component attempting to resolve it from the global state.
Alternatively, we could also pass in the sorting options and the caller would be responsible for generating them based the current state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can usePostTypes not be updated for this purpose? https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/query/utils.js#L101
Thank you for working on this. In the block editor when you edit a page and change the order, the label for the option is just "Order", but "Order by order" is not a helpful label 😆 |
Thank you for reviewing the changeset @carolinan!
Right!? That is why I left it at "menu order" because I couldn't think of a better term that would work equally well between various post types. How about "Sort by manual order" or "Sort by custom order"? |
First, kudos for working on this, @kasparsd. Second, while the dropdown is totally native, I did wonder about bringing the same filtering now in from DataViews and the documentation in Storybook here. I don't want to derail this ticket though. Let's ship good things quickly. I will make another one as I feel some unification can be done. Beyond that, thank you again; it's so great to see. |
Thank you @karmatosed for reviewing the ticket and providing the additional context!
I had the exact same feeling when I saw the ordering options repeated in three different places across the codebase. It feels like there must be some historic context that would explain that and potentially an existing initiative that is working to resolve this. So I'm openly asking for support for (1) how to bring in the contextual availability for the |
It also can't use "Page order" since it can be enabled on other post types. |
Thanks for the additional context @karmatosed! I've updated the labels per your recommendation. I believe this is the best we can do without revisiting the whole "menu order" label across the admin (quick edit, page order configuration, etc.). |
Duplicate: #51290 |
Thanks for finding the prior attempts at this @carolinan! I've responded inline #51290 with the requirements for the custom order availability. |
}, | ||
]; | ||
function OrderControl( { order, orderBy, onChange } ) { | ||
function OrderControl( { order, orderBy, orderByOptions, onChange } ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We keep this component decoupled from any editor state and expect to receive the orderByOptions
from the user similar to the order
and orderBy
.
Question: Is this a breaking change and should we consider an upgrade path for anyone using this component? Now, without the orderByOptions
value it will render an empty selector.
* @param {string} postType The post type to check. | ||
* @return {OrderByOption[]} List of order options. | ||
*/ | ||
export function useOrderByOptions( postType ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new helper method that returns all of the ordering options for a post type, accounting for the page-attributes
support.
@@ -45,7 +45,17 @@ export type AuthorSelectProps = Pick< | |||
}; | |||
|
|||
type Order = 'asc' | 'desc'; | |||
type OrderBy = 'date' | 'title'; | |||
type OrderBy = 'date' | 'title' | 'menu_order'; | |||
type OrderByOption = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this type definition match the project conventions?
What?
Add the
menu_order
sorting option in addition to post title and date.Why?
Fixes #42710.
Users are able to set a numerical menu order for post types that support it (pages, by default). Smaller values rise to the top while larger values sink to the bottom, as implemented by
WP_Query
ordering.How?
QueryControls
component to includemenu_order
as one of the options.Questions
While the
menu_order
value is available for all post types in the database, only those that register support forpage-attributes
feature will have the setting available. What's the best approach to including themenu_order
option only to post types with the page attributes enabled? Here is how the "Order" gets added conditionally to the dataviews:gutenberg/packages/editor/src/dataviews/store/private-actions.ts
Lines 156 to 158 in be5ce8a
Testing Instructions
Testing Instructions for Keyboard
This change is extending an existing selector so the keyboard navigation remains unchanged.
Screenshots or screencast
Ordering examples with updated preview:
Descending:
Ascending: