Make WordPress Core

Changeset 2396


Ignore:
Timestamp:
03/01/2005 09:10:12 AM (20 years ago)
Author:
matt
Message:

Category query speedup and indentation fixes. Hat tip: Scott. http://mosquito.wordpress.org/view.php?id=1005

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r2366 r2396  
    990990
    991991function query_posts($query) {
    992     global $wp_query;
    993 
    994     return $wp_query->query($query);
     992    global $wp_query;
     993    return $wp_query->query($query);
    995994}
    996995
    997996function update_post_caches($posts) {
    998     global $category_cache, $comment_count_cache, $post_meta_cache;
    999     global $wpdb;
    1000 
    1001     // No point in doing all this work if we didn't match any posts.
    1002     if (! $posts) {
    1003         return;
    1004     }
    1005 
    1006     // Get the categories for all the posts
    1007     foreach ($posts as $post)
    1008         $post_id_list[] = $post->ID;
    1009     $post_id_list = implode(',', $post_id_list);
    1010 
    1011     $dogs = $wpdb->get_results("SELECT DISTINCT
    1012         ID, category_id, cat_name, category_nicename, category_description, category_parent
    1013         FROM $wpdb->categories, $wpdb->post2cat, $wpdb->posts
    1014         WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)");
    1015        
    1016     if (!empty($dogs)) {
    1017         foreach ($dogs as $catt) {
    1018                     $category_cache[$catt->ID][$catt->category_id] = $catt;
    1019         }
    1020     }
    1021 
    1022     // Do the same for comment numbers
    1023     $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
    1024         FROM $wpdb->posts
    1025         LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID  AND comment_approved =  '1')
    1026         WHERE post_status =  'publish' AND ID IN ($post_id_list)
    1027         GROUP BY ID");
     997    global $category_cache, $comment_count_cache, $post_meta_cache;
     998    global $wpdb;
     999   
     1000    // No point in doing all this work if we didn't match any posts.
     1001    if ( !$posts )
     1002        return;
     1003
     1004    // Get the categories for all the posts
     1005    foreach ($posts as $post)
     1006        $post_id_list[] = $post->ID;
     1007    $post_id_list = implode(',', $post_id_list);
     1008
     1009    $dogs = $wpdb->get_results("SELECT DISTINCT
     1010    post_id, category_id, cat_name, category_nicename, category_description, category_parent
     1011    FROM $wpdb->categories, $wpdb->post2cat
     1012    WHERE category_id = cat_ID AND post_id IN ($post_id_list)");
    10281013   
    1029     if ($comment_counts) {
    1030         foreach ($comment_counts as $comment_count) {
    1031             $comment_count_cache["$comment_count->ID"] = $comment_count->ccount;
    1032         }
    1033     }
     1014    if ( !empty($dogs) ) {
     1015        foreach ($dogs as $catt) {
     1016            $category_cache[$catt->post_id][$catt->category_id] = $catt;
     1017        }
     1018    }
     1019
     1020    // Do the same for comment numbers
     1021    $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
     1022    FROM $wpdb->posts
     1023    LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID  AND comment_approved =  '1')
     1024    WHERE post_status =  'publish' AND ID IN ($post_id_list)
     1025    GROUP BY ID");
     1026   
     1027    if ($comment_counts) {
     1028        foreach ($comment_counts as $comment_count)
     1029            $comment_count_cache["$comment_count->ID"] = $comment_count->ccount;
     1030    }
    10341031
    10351032    // Get post-meta info
    1036     if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta  WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
    1037        
    1038         // Change from flat structure to hierarchical:
    1039         $post_meta_cache = array();
    1040         foreach ($meta_list as $metarow) {
    1041             $mpid = $metarow['post_id'];
    1042             $mkey = $metarow['meta_key'];
    1043             $mval = $metarow['meta_value'];
    1044            
    1045             // Force subkeys to be array type:
    1046             if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
    1047                 $post_meta_cache[$mpid] = array();
    1048             if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
    1049                 $post_meta_cache[$mpid]["$mkey"] = array();
    1050            
    1051             // Add a value to the current pid/key:
    1052             $post_meta_cache[$mpid][$mkey][] = $mval;
    1053         }
    1054     }
     1033    if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta  WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
     1034        // Change from flat structure to hierarchical:
     1035        $post_meta_cache = array();
     1036        foreach ($meta_list as $metarow) {
     1037            $mpid = $metarow['post_id'];
     1038            $mkey = $metarow['meta_key'];
     1039            $mval = $metarow['meta_value'];
     1040
     1041            // Force subkeys to be array type:
     1042            if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
     1043                $post_meta_cache[$mpid] = array();
     1044            if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
     1045                $post_meta_cache[$mpid]["$mkey"] = array();
     1046
     1047            // Add a value to the current pid/key:
     1048            $post_meta_cache[$mpid][$mkey][] = $mval;
     1049        }
     1050    }
    10551051}
    10561052
    10571053function update_category_cache() {
    1058     global $cache_categories, $wpdb;
    1059     $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories");
    1060     foreach ($dogs as $catt) {
    1061         $cache_categories[$catt->cat_ID] = $catt;
    1062     }
     1054    global $cache_categories, $wpdb;
     1055    $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories");
     1056    foreach ($dogs as $catt)
     1057        $cache_categories[$catt->cat_ID] = $catt;
    10631058}
    10641059
    10651060function update_user_cache() {
    1066     global $cache_userdata, $wpdb;
    1067 
    1068     if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0") ) :
     1061    global $cache_userdata, $wpdb;
     1062   
     1063    if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0") ) :
    10691064        foreach ($users as $user) :
    10701065            $cache_userdata[$user->ID] = $user;
     
    10721067        endforeach;
    10731068        return true;
    1074     else:
     1069    else :
    10751070        return false;
    10761071    endif;
  • trunk/wp-includes/template-functions-category.php

    r2387 r2396  
    44    global $post, $wpdb, $category_cache;
    55
    6     if (! $id) {
    7         $id = $post->ID;
    8     }
    9 
    10     if ($category_cache[$id]) {
    11              $categories = $category_cache[$id];
     6    if ( !$id )
     7        $id = $post->ID;
     8
     9    if ( $category_cache[$id] ) {
     10        $categories = $category_cache[$id];
    1211    } else {
    13         $categories = $wpdb->get_results("
    14             SELECT category_id, cat_name, category_nicename, category_description, category_parent
    15             FROM  $wpdb->categories, $wpdb->post2cat
    16             WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id'
    17             ");
    18    
    19     }
    20 
    21         if (!empty($categories))
    22             sort($categories);
    23         else
    24             $categories = array();
    25 
    26         return $categories;
     12        $categories = $wpdb->get_results("
     13        SELECT category_id, cat_name, category_nicename, category_description, category_parent
     14        FROM  $wpdb->categories, $wpdb->post2cat
     15        WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id'
     16        ");
     17    }
     18
     19    if (!empty($categories))
     20        sort($categories);
     21    else
     22        $categories = array();
     23
     24    return $categories;
    2725}
    2826
Note: See TracChangeset for help on using the changeset viewer.