From 250ecaaf4f1021add77a4cf7e4f9caee94f72ca3 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Sat, 24 Dec 2005 16:26:55 +0000 Subject: [PATCH] Fix page permalink 404 when pages are reordered. Props David House. fixes #2071 git-svn-id: https://develop.svn.wordpress.org/trunk@3356 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/classes.php | 2 +- wp-includes/functions-post.php | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/wp-includes/classes.php b/wp-includes/classes.php index 8589460d66..3cf1dffbc2 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -370,7 +370,7 @@ class WP_Query { $page_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); $all_page_ids = get_all_page_ids(); - $reqpage = 0; + $reqpage = 0; foreach ( $all_page_ids as $page_id ) { $page = get_page($page_id); if ( $page->fullpath == $page_path ) { diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php index 8636a3c197..916d1e7f1e 100644 --- a/wp-includes/functions-post.php +++ b/wp-includes/functions-post.php @@ -769,18 +769,37 @@ function add_ping($post_id, $uri) { // Add a URI to those already pung return $wpdb->query("UPDATE $wpdb->posts SET pinged = '$new' WHERE ID = $post_id"); } +//fetches the pages returned as a FLAT list, but arranged in order of their hierarchy, i.e., child parents +//immediately follow their parents +function get_page_hierarchy($posts, $parent = 0) { + $result = array ( ); + if ($posts) { foreach ($posts as $post) { + if ($post->post_parent == $parent) { + $result[$post->ID] = $post->post_name; + $children = get_page_hierarchy($posts, $post->ID); + $result += $children; //append $children to $result + } + } } + return $result; +} + function generate_page_rewrite_rules() { global $wpdb; - $posts = $wpdb->get_results("SELECT ID, post_name FROM $wpdb->posts WHERE post_status = 'static' ORDER BY post_parent DESC"); + + //get pages in order of hierarchy, i.e. children after parents + $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_status = 'static'")); + //now reverse it, because we need parents after children for rewrite rules to work properly + $posts = array_reverse($posts, true); $page_rewrite_rules = array(); if ($posts) { - foreach ($posts as $post) { + + foreach ($posts as $id => $post) { // URI => page name - $uri = get_page_uri($post->ID); + $uri = get_page_uri($id); - $page_rewrite_rules[$uri] = $post->post_name; + $page_rewrite_rules[$uri] = $post; } update_option('page_uris', $page_rewrite_rules);