From 992ad226e2c525b9b217eb5dbf918cff5566e4de Mon Sep 17 00:00:00 2001 From: Rafael Funchal Date: Mon, 30 Nov 2026 10:02:25 -0300 Subject: [PATCH 1/3] Posts, Post Types: Add a `fields` argument to `get_pages()` for page dropdowns. With `'fields' => 'dropdown'`, `get_pages()` runs the same `WP_Query` for IDs only, then loads only the columns a page dropdown needs. The rows are cached separately and kept out of the post cache, so a later `get_post()` still returns the full page. Without the argument nothing changes, including the arguments passed to the `get_pages_query_args` and `get_pages` filters. See #41714. --- src/wp-includes/post.php | 70 ++++++++++ tests/phpunit/tests/post/getPages.php | 135 +++++++++++++++++++ tests/phpunit/tests/post/wpDropdownPages.php | 48 +++++++ 3 files changed, 253 insertions(+) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 6d42c97836dea..f41bf1b662cad 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -6755,6 +6755,7 @@ function get_page_uri( $page = 0 ) { * * @since 1.5.0 * @since 6.3.0 Use WP_Query internally. + * @since 7.3.0 Added the `$fields` argument. * * @param array|string $args { * Optional. Array or string of arguments to retrieve pages. @@ -6788,6 +6789,13 @@ function get_page_uri( $page = 0 ) { * @type string $post_type The post type to query. Default 'page'. * @type string|array $post_status A comma-separated list or array of post statuses to include. * Default 'publish'. + * @type string $fields Which fields to load. Accepts 'all' for complete page objects, or + * 'dropdown' for page objects with only ID, post_author, post_date, + * post_date_gmt, post_title, post_status, post_name, post_parent, + * menu_order, and post_type. With 'dropdown', other fields such as + * post_content are empty, including in callbacks on the 'get_pages' + * filter, and the pages are not added to the post cache. + * Default 'all'. * } * @return WP_Post[]|false Array of pages (or hierarchical post type items). Boolean false if the * specified post type is not hierarchical or the specified status is not @@ -6814,6 +6822,9 @@ function get_pages( $args = array() ) { $parsed_args = wp_parse_args( $args, $defaults ); + // Not in $defaults, so that the arguments passed to the filters below are unchanged for existing callers. + $dropdown_fields = isset( $parsed_args['fields'] ) && 'dropdown' === $parsed_args['fields']; + $number = (int) $parsed_args['number']; $offset = (int) $parsed_args['offset']; $child_of = (int) $parsed_args['child_of']; @@ -6926,6 +6937,10 @@ static function ( $orderby_field ) { $query_args['posts_per_page'] = $number; } + if ( $dropdown_fields ) { + $query_args['fields'] = 'ids'; + } + /** * Filters query arguments passed to WP_Query in get_pages(). * @@ -6939,6 +6954,11 @@ static function ( $orderby_field ) { $pages = new WP_Query(); $pages = $pages->query( $query_args ); + // A 'get_pages_query_args' callback may have removed 'fields', in which case the pages are complete. + if ( $dropdown_fields && isset( $query_args['fields'] ) && 'ids' === $query_args['fields'] ) { + $pages = _get_pages_dropdown_fields( $pages ); + } + if ( $child_of || $hierarchical ) { $pages = get_page_children( $child_of, $pages ); } @@ -6971,6 +6991,56 @@ static function ( $orderby_field ) { return apply_filters( 'get_pages', $pages, $parsed_args ); } +/** + * Loads pages with only the fields a page dropdown needs. + * + * Used by get_pages() when `$fields` is 'dropdown'. The rows are cached on their own and never + * added to the post cache, so a later get_post() call still loads the complete page. + * + * @since 7.3.0 + * @access private + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @param int[] $page_ids Page IDs, in the order to return them. + * @return WP_Post[] Pages with only the dropdown fields set, in the order of `$page_ids`. + */ +function _get_pages_dropdown_fields( $page_ids ) { + global $wpdb; + + $page_ids = array_map( 'intval', $page_ids ); + if ( empty( $page_ids ) ) { + return array(); + } + + $last_changed = wp_cache_get_last_changed( 'posts' ); + $cache_key = 'get_pages_dropdown_fields:' . md5( implode( ',', $page_ids ) ); + $rows = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed ); + + if ( false === $rows ) { + $rows = array(); + foreach ( array_chunk( $page_ids, 1000 ) as $chunk ) { + $ids = implode( ',', $chunk ); + $rows += $wpdb->get_results( + "SELECT ID, post_author, post_date, post_date_gmt, post_title, post_status, post_name, post_parent, menu_order, post_type + FROM $wpdb->posts WHERE ID IN ($ids)", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $ids holds integers only. + OBJECT_K + ); + } + wp_cache_set_salted( $cache_key, $rows, 'post-queries', $last_changed ); + } + + $pages = array(); + foreach ( $page_ids as $page_id ) { + if ( isset( $rows[ $page_id ] ) ) { + // Clone so that get_post() sanitizing the row doesn't change the cached copy. + $pages[] = get_post( clone $rows[ $page_id ] ); + } + } + + return $pages; +} + // // Attachment functions. // diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index a0a1d209957bd..deda17c626d5f 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -1204,4 +1204,139 @@ public function test_get_pages_order_by_post_modified_gmt() { 'Check that ORDER is post modified when using modified_gmt.' ); } + + /** + * @ticket 41714 + * + * @dataProvider data_dropdown_fields_args + * + * @param array $args Arguments for get_pages(), with `true` standing for the parent page ID. + */ + public function test_dropdown_fields_return_the_same_pages_in_the_same_order( $args ) { + $parent_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Parent', + 'menu_order' => 2, + ) + ); + $child_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Child', + 'post_parent' => $parent_id, + ) + ); + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Grandchild', + 'post_parent' => $child_id, + ) + ); + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Another', + 'menu_order' => 1, + ) + ); + foreach ( array( 'child_of', 'exclude_tree' ) as $key ) { + if ( isset( $args[ $key ] ) ) { + $args[ $key ] = 'child_of' === $key ? $parent_id : $child_id; + } + } + + $full = get_pages( $args ); + $light = get_pages( array_merge( $args, array( 'fields' => 'dropdown' ) ) ); + + $this->assertNotEmpty( $full ); + $this->assertSame( wp_list_pluck( $full, 'ID' ), wp_list_pluck( $light, 'ID' ) ); + $this->assertSame( wp_list_pluck( $full, 'post_title' ), wp_list_pluck( $light, 'post_title' ) ); + $this->assertSame( wp_list_pluck( $full, 'post_parent' ), wp_list_pluck( $light, 'post_parent' ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_dropdown_fields_args() { + return array( + 'defaults' => array( array() ), + 'flat' => array( array( 'hierarchical' => false ) ), + 'menu order' => array( array( 'sort_column' => 'menu_order, post_title' ) ), + 'descending' => array( array( 'sort_order' => 'DESC' ) ), + 'child of' => array( array( 'child_of' => true ) ), + 'exclude tree' => array( array( 'exclude_tree' => true ) ), + 'number' => array( + array( + 'number' => 2, + 'hierarchical' => false, + ), + ), + ); + } + + /** + * @ticket 41714 + */ + public function test_dropdown_fields_leave_out_content_and_the_post_cache() { + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'A page', + 'post_content' => 'Long content', + ) + ); + clean_post_cache( $page_id ); + + $pages = get_pages( array( 'fields' => 'dropdown' ) ); + + $this->assertSame( array( $page_id ), wp_list_pluck( $pages, 'ID' ) ); + $this->assertSame( 'A page', $pages[0]->post_title ); + $this->assertSame( '', $pages[0]->post_content ); + $this->assertFalse( wp_cache_get( $page_id, 'posts' ), 'A partial page must not be cached as the full page.' ); + $this->assertSame( 'Long content', get_post( $page_id )->post_content ); + } + + /** + * @ticket 41714 + */ + public function test_dropdown_fields_are_cached_until_pages_change() { + self::factory()->post->create( array( 'post_type' => 'page' ) ); + + $first = get_pages( array( 'fields' => 'dropdown' ) ); + $num_queries = get_num_queries(); + $second = get_pages( array( 'fields' => 'dropdown' ) ); + + $this->assertSame( $num_queries, get_num_queries(), 'The second call should be served from the cache.' ); + $this->assertSame( wp_list_pluck( $first, 'post_title' ), wp_list_pluck( $second, 'post_title' ) ); + + $new_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); + $this->assertContains( $new_id, wp_list_pluck( get_pages( array( 'fields' => 'dropdown' ) ), 'ID' ) ); + } + + /** + * @ticket 41714 + */ + public function test_dropdown_fields_return_full_pages_when_a_filter_removes_the_ids_query() { + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_content' => 'Long content', + ) + ); + add_filter( + 'get_pages_query_args', + static function ( $query_args ) { + unset( $query_args['fields'] ); + return $query_args; + } + ); + + $pages = get_pages( array( 'fields' => 'dropdown' ) ); + + $this->assertSame( 'Long content', $pages[0]->post_content ); + } } diff --git a/tests/phpunit/tests/post/wpDropdownPages.php b/tests/phpunit/tests/post/wpDropdownPages.php index d838875f1d315..4a99e7f5a5b2e 100644 --- a/tests/phpunit/tests/post/wpDropdownPages.php +++ b/tests/phpunit/tests/post/wpDropdownPages.php @@ -219,4 +219,52 @@ public function test_wp_dropdown_pages_should_obey_class_parameter() { $this->assertMatchesRegularExpression( '/]+class=\'bar\'/', $found ); } + + /** + * @ticket 41714 + */ + public function test_wp_dropdown_pages_with_dropdown_fields_renders_the_same_options_without_loading_content() { + global $wpdb; + + $parent_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Parent', + 'post_content' => 'Long content', + ) + ); + $child_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Child', + 'post_parent' => $parent_id, + ) + ); + $bump = '   '; + $expected = "\n"; + + $queries = array(); + $collect = static function ( $query ) use ( &$queries ) { + $queries[] = $query; + return $query; + }; + add_filter( 'query', $collect ); + $found = wp_dropdown_pages( + array( + 'echo' => 0, + 'selected' => $child_id, + 'show_option_none' => 'None', + 'option_none_value' => '0', + 'fields' => 'dropdown', + ) + ); + remove_filter( 'query', $collect ); + + $this->assertSame( $expected, $found ); + $this->assertStringNotContainsString( "{$wpdb->posts}.*", implode( "\n", $queries ) ); + } } From 49f5a6d5d818a9d403e8223d156102ac3c03b5ad Mon Sep 17 00:00:00 2001 From: Rafael Funchal Date: Mon, 30 Nov 2026 10:02:25 -0300 Subject: [PATCH 2/3] Administration: Load core's page dropdowns with `get_pages()`' dropdown fields. Settings > Reading, the Privacy policy page selector, Page Attributes, Quick Edit and the Customizer's page control only show titles and hierarchy, but loaded every page in full. On sites with thousands of long pages, these screens run out of memory. See #41714. --- src/wp-admin/includes/class-wp-posts-list-table.php | 1 + src/wp-admin/includes/meta-boxes.php | 1 + src/wp-admin/options-privacy.php | 1 + src/wp-admin/options-reading.php | 2 ++ src/wp-includes/class-wp-customize-control.php | 1 + 5 files changed, 6 insertions(+) diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php index c7388b63df5c9..cd6fccefb9779 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -1935,6 +1935,7 @@ public function inline_edit() { 'show_option_none' => __( 'Main Page (no parent)' ), 'option_none_value' => 0, 'sort_column' => 'menu_order, post_title', + 'fields' => 'dropdown', ); if ( $bulk ) { diff --git a/src/wp-admin/includes/meta-boxes.php b/src/wp-admin/includes/meta-boxes.php index 54367825514f6..719fd8b119893 100644 --- a/src/wp-admin/includes/meta-boxes.php +++ b/src/wp-admin/includes/meta-boxes.php @@ -1009,6 +1009,7 @@ function page_attributes_meta_box( $post ) { 'show_option_none' => __( '(no parent)' ), 'sort_column' => 'menu_order, post_title', 'echo' => 0, + 'fields' => 'dropdown', ); /** diff --git a/src/wp-admin/options-privacy.php b/src/wp-admin/options-privacy.php index 739c8edab1cda..8b8726c7e5d4f 100644 --- a/src/wp-admin/options-privacy.php +++ b/src/wp-admin/options-privacy.php @@ -313,6 +313,7 @@ static function ( $body_class ) { 'option_none_value' => '0', 'selected' => $privacy_policy_page_id, 'post_status' => array( 'draft', 'publish' ), + 'fields' => 'dropdown', ) ); diff --git a/src/wp-admin/options-reading.php b/src/wp-admin/options-reading.php index 8ea14a7178256..9dbcdd33757db 100644 --- a/src/wp-admin/options-reading.php +++ b/src/wp-admin/options-reading.php @@ -120,6 +120,7 @@ 'show_option_none' => __( '— Select —' ), 'option_none_value' => '0', 'selected' => get_option( 'page_on_front' ), + 'fields' => 'dropdown', ) ) ); @@ -137,6 +138,7 @@ 'show_option_none' => __( '— Select —' ), 'option_none_value' => '0', 'selected' => get_option( 'page_for_posts' ), + 'fields' => 'dropdown', ) ) ); diff --git a/src/wp-includes/class-wp-customize-control.php b/src/wp-includes/class-wp-customize-control.php index 859958a2f6838..8775c0ed4ef78 100644 --- a/src/wp-includes/class-wp-customize-control.php +++ b/src/wp-includes/class-wp-customize-control.php @@ -604,6 +604,7 @@ protected function render_content() { 'show_option_none' => $show_option_none, 'option_none_value' => $option_none_value, 'selected' => $this->value(), + 'fields' => 'dropdown', ) ); if ( empty( $dropdown ) ) { From b1d22dfd540a91ee56d9c9d9c0d2503be4a64db3 Mon Sep 17 00:00:00 2001 From: Rafael Funchal Date: Mon, 30 Nov 2026 10:02:25 -0300 Subject: [PATCH 3/3] Administration: Check for one top-level page on the Reading Settings screen. Settings > Reading decided whether to show the homepage settings with `get_pages()`, which loads every published page in full and adds them all to the post cache. Check for one published top-level page instead. The default, hierarchical `get_pages()` result is non-empty only when such a page exists, and the homepage dropdowns offer the same pages, so the answer is unchanged. With the dropdowns' `fields => 'dropdown'`, the screen no longer loads any page in full. See #41714. --- src/wp-admin/options-reading.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/options-reading.php b/src/wp-admin/options-reading.php index 9dbcdd33757db..5624fd8e8434a 100644 --- a/src/wp-admin/options-reading.php +++ b/src/wp-admin/options-reading.php @@ -69,9 +69,22 @@ if ( ! is_utf8_charset() ) { add_settings_field( 'blog_charset', __( 'Encoding for pages and feeds' ), 'options_reading_blog_charset', 'reading', 'default', array( 'label_for' => 'blog_charset' ) ); } + +/* + * Check for one published top-level page instead of loading every page. + * The hierarchical get_pages() result is only non-empty when such a page + * exists, and the homepage dropdowns below offer the same pages. + */ +$has_pages = (bool) get_pages( + array( + 'number' => 1, + 'parent' => 0, + 'hierarchical' => false, + ) +); ?> - +