Skip to content
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

Query Loop: Add Page Order (menu_order) #51290

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/compat/wordpress-6.2/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function gutenberg_pattern_directory_collection_params_6_2( $query_params ) {
'include_slugs',
'title',
'favorite_count',
'menu_order',
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export default function QueryInspectorControls( props ) {
if ( newValue !== 'post' ) {
updateQuery.sticky = '';
}

// We need to reset `orderBy` and `order` to their default values if `orderBy` is set to `menu_order` because only pages support this ordering by default
if ( orderBy === 'menu_order' && newValue !== 'page' ) {
updateQuery.orderBy = 'date';
updateQuery.order = 'desc';
}
// We need to reset `parents` because they are tied to each post type.
updateQuery.parents = [];
setQuery( updateQuery );
Expand Down Expand Up @@ -190,7 +196,7 @@ export default function QueryInspectorControls( props ) {
) }
{ showOrderControl && (
<OrderControl
{ ...{ order, orderBy } }
{ ...{ order, orderBy, postType } }
onChange={ setQuery }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@
import { SelectControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

const orderOptions = [
{
label: __( 'Newest to oldest' ),
value: 'date/desc',
},
{
label: __( 'Oldest to newest' ),
value: 'date/asc',
},
{
/* translators: label for ordering posts by title in ascending order */
label: __( 'A → Z' ),
value: 'title/asc',
},
{
/* translators: label for ordering posts by title in descending order */
label: __( 'Z → A' ),
value: 'title/desc',
},
];
function OrderControl( { order, orderBy, onChange } ) {
function OrderControl( { order, orderBy, postType, onChange } ) {
const orderOptions = [
{
label: __( 'Newest to oldest' ),
value: 'date/desc',
},
{
label: __( 'Oldest to newest' ),
value: 'date/asc',
},
{
/* translators: label for ordering posts by title in ascending order */
label: __( 'A → Z' ),
value: 'title/asc',
},
{
/* translators: label for ordering posts by title in descending order */
label: __( 'Z → A' ),
value: 'title/desc',
},
// Only include the menu_order option for pages
...( postType === 'page'
? [
{
label: __( 'Page Order' ),
value: 'menu_order/asc',
},
]
: [] ),
];

return (
<SelectControl
__nextHasNoMarginBottom
Expand Down