From 3d2f4d5a88ab33e21f0ae97c277ddabb066c886b Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 29 Aug 2023 22:03:25 +0000 Subject: [PATCH] Posts, Post Types: Reinstate missing sort_column options in get_pages(). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes an issue introduced in [55569] that broke sort ordering by `post_modified_gmt` or `modified_gmt`. Props david.binda, azaozz, spacedmonkey, flixos90, joemcgill. Fixes #59226. git-svn-id: https://develop.svn.wordpress.org/trunk@56490 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 16 +++++++++++++- tests/phpunit/tests/post/getPages.php | 31 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 5367f51e06..0bd4fc7a9f 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -6066,8 +6066,22 @@ function get_pages( $args = array() ) { $query_args['post_parent'] = $parent; } + /* + * Maintain backward compatibility for `sort_column` key. + * Additionally to `WP_Query`, it has been supporting the `post_modified_gmt` field, so this logic will translate + * it to `post_modified` which should result in the same order given the two dates in the fields match. + */ $orderby = wp_parse_list( $parsed_args['sort_column'] ); - $orderby = array_map( 'trim', $orderby ); + $orderby = array_map( + static function( $orderby_field ) { + $orderby_field = trim( $orderby_field ); + if ( 'post_modified_gmt' === $orderby_field || 'modified_gmt' === $orderby_field ) { + $orderby_field = str_replace( '_gmt', '', $orderby_field ); + } + return $orderby_field; + }, + $orderby + ); if ( $orderby ) { $query_args['orderby'] = array_fill_keys( $orderby, $parsed_args['sort_order'] ); } diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index 92de3356fa..e72ba3e2ed 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -1181,4 +1181,35 @@ class Tests_Post_GetPages extends WP_UnitTestCase { 'Check that ORDER is post date.' ); } + + /** + * Tests that the legacy `post_modified_gmt` orderby values are translated to the proper `WP_Query` values. + * + * @ticket 59226 + */ + public function test_get_pages_order_by_post_modified_gmt() { + global $wpdb; + + get_pages( + array( + 'sort_column' => 'post_modified_gmt', + ) + ); + $this->assertStringContainsString( + "ORDER BY $wpdb->posts.post_modified ASC", + $wpdb->last_query, + 'Check that ORDER is post modified when using post_modified_gmt.' + ); + + get_pages( + array( + 'sort_column' => 'modified_gmt', + ) + ); + $this->assertStringContainsString( + "ORDER BY $wpdb->posts.post_modified ASC", + $wpdb->last_query, + 'Check that ORDER is post modified when using modified_gmt.' + ); + } }