From b361854d4f5f185ca512046a0f9aae6efcefc71b Mon Sep 17 00:00:00 2001 From: michelvaldrighi Date: Wed, 16 Feb 2005 15:12:04 +0000 Subject: [PATCH] added cache_pages to avoid making 1+X queries everytime wp_list_pages is called, where X is the number of pages git-svn-id: http://svn.automattic.com/wordpress/trunk@2354 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 10 ++-- wp-includes/template-functions-post.php | 69 ++++++++++++++----------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 6dc2c2a32f..74ab41dd25 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -941,10 +941,14 @@ function remove_action($tag, $function_to_remove, $priority = 10) { remove_filter($tag, $function_to_remove, $priority); } -function get_page_uri($page) { - global $wpdb; - $page = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$page'"); +function get_page_uri($page_id) { + global $wpdb, $cache_pages; + if (!isset($cache_pages[$page_id])) { + $cache_pages[$page_id] = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$page_id'"); + } + + $page = $cache_pages[$page_id]; $uri = urldecode($page->post_name); // A page cannot be it's own parent. diff --git a/wp-includes/template-functions-post.php b/wp-includes/template-functions-post.php index 1b6ace90f1..b4978f38b1 100644 --- a/wp-includes/template-functions-post.php +++ b/wp-includes/template-functions-post.php @@ -260,42 +260,50 @@ function the_meta() { // function get_pages($args = '') { - global $wpdb; + global $wpdb, $cache_pages; - parse_str($args, $r); + if (!isset($cache_pages) || empty($cache_pages)) { - if (!isset($r['child_of'])) $r['child_of'] = 0; - if (!isset($r['sort_column'])) $r['sort_column'] = 'post_title'; - if (!isset($r['sort_order'])) $r['sort_order'] = 'ASC'; + parse_str($args, $r); - $exclusions = ''; - if (!empty($r['exclude'])) { - $expages = preg_split('/[\s,]+/',$r['exclude']); - if (count($expages)) { - foreach ($expages as $expage) { - $exclusions .= ' AND ID <> ' . intval($expage) . ' '; + if (!isset($r['child_of'])) $r['child_of'] = 0; + if (!isset($r['sort_column'])) $r['sort_column'] = 'post_title'; + if (!isset($r['sort_order'])) $r['sort_order'] = 'ASC'; + + $exclusions = ''; + if (!empty($r['exclude'])) { + $expages = preg_split('/[\s,]+/',$r['exclude']); + if (count($expages)) { + foreach ($expages as $expage) { + $exclusions .= ' AND ID <> ' . intval($expage) . ' '; + } } } + + $dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified"; + $dates .= ",UNIX_TIMESTAMP(post_date) AS time_created"; + + $post_parent = ''; + if ($r['child_of']) { + $post_parent = ' AND post_parent=' . $r['child_of'] . ' '; + } + + $pages = $wpdb->get_results("SELECT " . + "ID, post_title, post_name, post_parent " . + "$dates " . + "FROM $wpdb->posts " . + "WHERE post_status = 'static' " . + "$post_parent" . + "$exclusions " . + "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']); + + foreach($pages as $page) { + $cache_pages[$page->ID] = $page; + } + } - $dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified"; - $dates .= ",UNIX_TIMESTAMP(post_date) AS time_created"; - - $post_parent = ''; - if ($r['child_of']) { - $post_parent = ' AND post_parent=' . $r['child_of'] . ' '; - } - - $pages = $wpdb->get_results("SELECT " . - "ID, post_title,post_parent " . - "$dates " . - "FROM $wpdb->posts " . - "WHERE post_status = 'static' " . - "$post_parent" . - "$exclusions " . - "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']); - - return $pages; + return $cache_pages; } function wp_list_pages($args = '') { @@ -317,6 +325,7 @@ function wp_list_pages($args = '') { foreach($pages as $page) { // set the title for the current page $page_tree[$page->ID]['title'] = $page->post_title; + $page_tree[$page->ID]['name'] = $page->post_name; // set the selected date for the current page // depending on the query arguments this is either @@ -335,7 +344,7 @@ function wp_list_pages($args = '') { // array index we set the curent page as a child of that page. // We can now start looping over the $page_tree array // with any ID which will output the page links from that ID downwards. - $page_tree[$page->post_parent]['children'][] = $page->ID; + $page_tree[$page->post_parent]['children'][] = $page->ID; } // Output of the pages starting with child_of as the root ID. // child_of defaults to 0 if not supplied in the query.