Commit 01bc4ed877 for wordpress.org
commit 01bc4ed8770598e2e428ba60811c0e02e36c20ef
Author: lancewillett <lancewillett@git.wordpress.org>
Date: Fri Oct 30 17:19:56 2026 +0000
General: Preserve Walker hierarchy when children sort first.
When no top-level element is included, select the first element whose parent is absent from the input instead of assuming the first element is a root. Use the configured `db_fields` and retain the first-element fallback for cycles.
This preserves the hierarchy and depth limits when `wp_list_pages()` sorts descendants before their included ancestors.
Developed in: https://github.com/WordPress/wordpress-develop/pull/14684
Props rob-castlegate, salvatorecorsi, mukesh27.
Fixes #28801.
Built from https://develop.svn.wordpress.org/trunk@65060
git-svn-id: http://core.svn.wordpress.org/trunk@64210 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-walker.php b/wp-includes/class-wp-walker.php
index 3937d5b89d..2c40984119 100644
--- a/wp-includes/class-wp-walker.php
+++ b/wp-includes/class-wp-walker.php
@@ -230,11 +230,23 @@ class Walker {
/*
* When none of the elements is top level.
- * Assume the first one must be root of the sub elements.
+ * Find an element whose parent is not in the list to use as the root.
*/
if ( empty( $top_level_elements ) ) {
+ $id_field = $this->db_fields['id'];
+ $element_ids = array();
+ foreach ( $elements as $e ) {
+ $element_ids[ $e->$id_field ] = true;
+ }
+ // Fall back to the first element when all parents are included, as with a cycle.
$root = array_first( $elements );
+ foreach ( $elements as $e ) {
+ if ( ! isset( $element_ids[ $e->$parent_field ] ) ) {
+ $root = $e;
+ break;
+ }
+ }
$top_level_elements = array();
$children_elements = array();
diff --git a/wp-includes/version.php b/wp-includes/version.php
index e652c45cb9..947f42facb 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-beta3-65059';
+$wp_version = '7.2-beta3-65060';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.