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
This commit is contained in:
michelvaldrighi 2005-02-16 15:12:04 +00:00
parent 8e770d3e6e
commit b361854d4f
2 changed files with 46 additions and 33 deletions

View File

@ -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.

View File

@ -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.