@@ -441,7 +441,9 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
441441 $ orderby = $ args ['orderby ' ] ?? static ::uid_column ();
442442 $ order = strtoupper ( $ args ['order ' ] ?? 'ASC ' );
443443
444- if ( ! in_array ( $ orderby , static ::get_columns ()->get_names (), true ) ) {
444+ $ column_names = static ::get_columns ()->get_names ();
445+
446+ if ( ! in_array ( $ orderby , $ column_names , true ) ) {
445447 $ orderby = static ::uid_column ();
446448 }
447449
@@ -453,7 +455,10 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
453455
454456 [ $ join , $ secondary_columns ] = $ is_join ? static ::get_join_parts ( $ join_table , $ join_condition , $ selectable_joined_columns ) : [ '' , '' ];
455457
456- $ columns = implode ( ', ' , array_map ( fn ( $ column ) => "a. {$ column }" , $ columns ) );
458+ sort ( $ columns );
459+ sort ( $ column_names );
460+
461+ $ formatted_columns = implode ( ', ' , array_map ( fn ( $ column ) => "a. {$ column }" , $ columns ) );
457462
458463 /**
459464 * Fires before the results of the query are fetched.
@@ -469,7 +474,7 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
469474
470475 $ results = $ database ::get_results (
471476 $ database ::prepare (
472- "SELECT {$ columns }{$ secondary_columns } FROM %i a {$ join } {$ where } ORDER BY a. {$ orderby } {$ order } LIMIT %d, %d " ,
477+ "SELECT {$ formatted_columns }{$ secondary_columns } FROM %i a {$ join } {$ where } ORDER BY a. {$ orderby } {$ order } LIMIT %d, %d " ,
473478 static ::table_name ( true ),
474479 $ offset ,
475480 $ per_page
@@ -479,7 +484,8 @@ public static function paginate( array $args, int $per_page = 20, int $page = 1,
479484
480485 $ results = array_map ( fn ( $ result ) => self ::amend_value_types ( $ result ), $ results );
481486
482- if ( [ '* ' === $ columns ] ) {
487+ if ( [ '* ' ] === $ columns || $ columns === $ column_names ) {
488+ // If we are querying for a full row, let's transform the results.
483489 $ results = array_map ( fn ( $ result ) => static ::transform_from_array ( $ result ), $ results );
484490 }
485491
0 commit comments