Commit 8bebb0bc91 for wordpress.org
commit 8bebb0bc919a21c280e9311f892d1f9d7c360118
Author: zieladam <zieladam@git.wordpress.org>
Date: Thu Sep 24 12:27:49 2026 +0000
Query: Prefer published pages in path lookups.
A draft and a published page can share the same path. With no SQL result order, `get_page_by_path()` could return the draft and cause a 404 for the published page.
Order candidates with `publish` first, other statuses next, and `draft`, `pending`, and `auto-draft` last. Use the lowest ID to break ties within each group.
When `$post_type` is an array, return the first full-path match instead of the last so the SQL order is respected. Keep the existing preference for the requested post type over attachments when `$post_type` is a string.
Add regression tests for string and array post type arguments.
Developed in: https://github.com/WordPress/wordpress-develop/pull/13655
Props brookedot, trepmal, SirLouen, sahilgidwani, rdelbem, vijendrajat, jonsurrell.
Fixes #61996.
Built from https://develop.svn.wordpress.org/trunk@63915
git-svn-id: http://core.svn.wordpress.org/trunk@63084 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/post.php b/wp-includes/post.php
index c268544418..5edc8a50f0 100644
--- a/wp-includes/post.php
+++ b/wp-includes/post.php
@@ -6391,6 +6391,9 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
FROM $wpdb->posts
WHERE post_name IN ($in_string)
AND post_type IN ($post_type_in_string)
+ ORDER BY
+ post_status = 'publish' DESC,
+ post_status IN ('draft', 'pending', 'auto-draft') ASC, ID ASC
";
/** @var array<object{ ID: string, post_name: string, post_parent: string, post_type: string }> $pages */
@@ -6422,7 +6425,17 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
&& $p->post_name === $revparts[ $count ]
) {
$found_id = $page->ID;
- if ( $page->post_type === $post_type ) {
+
+ /*
+ * A string like 'page' also searches attachments: /about/photo/ could be
+ * a child page or an attachment page, and this lookup handles both.
+ * Keep an attachment as a fallback, but keep looking for the requested
+ * type so an attachment cannot hide a page with the same path.
+ *
+ * An array is the exact list of types to search; no extra types are added.
+ * SQL already checks that list, so stop at the first full-path match.
+ */
+ if ( is_array( $post_type ) || $page->post_type === $post_type ) {
break;
}
}
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 1c3e6e6cc2..f0ee57d976 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-alpha-63914';
+$wp_version = '7.2-alpha-63915';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.