From a8f21030de6bafad3519b2c242897f0bd9f919a7 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 21 Jun 2024 09:54:33 -0400 Subject: [PATCH 1/2] Add name to the allowed fields for order by in the REST API --- includes/query-loop.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/query-loop.php b/includes/query-loop.php index 5be064b..547c4fb 100644 --- a/includes/query-loop.php +++ b/includes/query-loop.php @@ -58,7 +58,7 @@ function ( $pre_render, $parsed_block ) { 'query_loop_block_query_vars', function ( $default_query, $block ) { // Retrieve the query from the passed block context. - $block_query = $block->context['query'] ?? []; + $block_query = $block->context['query'] ?? array(); // Process all of the params $qpg = new Query_Params_Generator( $default_query, $block_query ); @@ -127,6 +127,7 @@ function add_more_sort_by( $query_params ) { $query_params['orderby']['enum'][] = 'rand'; $query_params['orderby']['enum'][] = 'post__in'; $query_params['orderby']['enum'][] = 'comment_count'; + $query_params['orderby']['enum'][] = 'name'; return $query_params; } From 7fe761c033634eef5ae0585d3b524e00fcc6452d Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 21 Jun 2024 09:54:52 -0400 Subject: [PATCH 2/2] Add name as an option and abstract out the list ot be sorted --- src/components/post-order-controls.js | 103 ++++++++++++++------------ 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/src/components/post-order-controls.js b/src/components/post-order-controls.js index df094f1..4ca267c 100644 --- a/src/components/post-order-controls.js +++ b/src/components/post-order-controls.js @@ -4,6 +4,57 @@ import { SelectControl, ToggleControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; +export const sortOptions = [ + { + label: __( 'Name', 'advanced-query-loop' ), + value: 'name', + }, + { + label: __( 'Author', 'advanced-query-loop' ), + value: 'author', + }, + { + label: __( 'Comment Count', 'advanced-query-loop' ), + value: 'comment_count', + }, + { + label: __( 'Date', 'advanced-query-loop' ), + value: 'date', + }, + { + label: __( 'Included Posts', 'advanced-query-loop' ), + value: 'post__in', + }, + { + label: __( 'Last Modified Date', 'advanced-query-loop' ), + value: 'modified', + }, + { + label: __( 'Menu Order', 'advanced-query-loop' ), + value: 'menu_order', + }, + { + label: __( 'Meta Value', 'advanced-query-loop' ), + value: 'meta_value', + }, + { + label: __( 'Meta Value Num', 'advanced-query-loop' ), + value: 'meta_value_num', + }, + { + label: __( 'Post ID', 'advanced-query-loop' ), + value: 'id', + }, + { + label: __( 'Random', 'advanced-query-loop' ), + value: 'rand', + }, + { + label: __( 'Title', 'advanced-query-loop' ), + value: 'title', + }, +]; + /** * PostOrderControls component * @@ -25,55 +76,9 @@ export const PostOrderControls = ( { attributes, setAttributes } ) => { ) : '' } - options={ [ - { - label: __( 'Author', 'advanced-query-loop' ), - value: 'author', - }, - { - label: __( 'Comment Count', 'advanced-query-loop' ), - value: 'comment_count', - }, - { - label: __( 'Date', 'advanced-query-loop' ), - value: 'date', - }, - { - label: __( 'Included Posts', 'advanced-query-loop' ), - value: 'post__in', - }, - { - label: __( - 'Last Modified Date', - 'advanced-query-loop' - ), - value: 'modified', - }, - { - label: __( 'Menu Order', 'advanced-query-loop' ), - value: 'menu_order', - }, - { - label: __( 'Meta Value', 'advanced-query-loop' ), - value: 'meta_value', - }, - { - label: __( 'Meta Value Num', 'advanced-query-loop' ), - value: 'meta_value_num', - }, - { - label: __( 'Post ID', 'advanced-query-loop' ), - value: 'id', - }, - { - label: __( 'Random', 'advanced-query-loop' ), - value: 'rand', - }, - { - label: __( 'Title', 'advanced-query-loop' ), - value: 'title', - }, - ] } + options={ sortOptions.sort( ( a, b ) => + a.label.localeCompare( b.label ) + ) } onChange={ ( newOrderBy ) => { setAttributes( { query: {