diff --git a/class-object-type-google-sheet.php b/class-object-type-google-sheet.php index 03e0640..1ea7c12 100644 --- a/class-object-type-google-sheet.php +++ b/class-object-type-google-sheet.php @@ -322,6 +322,7 @@ public function query( $args ) { * @var $primary_property_value * @var $property * @var $property_id + * @var $ordering */ extract( $args ); @@ -334,6 +335,17 @@ public function query( $args ) { } ); } + $orderby = rgar( $ordering, 'orderby' ); + $order = strtolower( rgar( $ordering, 'order', 'ASC' ) ); + + if ( ! empty( $orderby ) && count( $results ) && array_key_exists( $orderby, $results[0] ) ) { + if ( $order === 'rand' ) { + shuffle( $results ); + } else { + array_multisort( array_column( $results, $orderby ), $order === 'desc' ? SORT_DESC : SORT_ASC, $results ); + } + } + $query_limit = gp_populate_anything()->get_query_limit( $this, $args['field'] ); $query_results = array_slice( $results, 0, $query_limit );