Make WordPress Core


Ignore:
Timestamp:
10/24/2006 10:52:59 AM (19 years ago)
Author:
markjaquith
Message:

Fix post meta caching system to reduce queries and eliminate redundant WP code. fixes #3273

File:
1 edited

Legend:

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

    r4383 r4419  
    564564    update_post_category_cache($post_id_list);
    565565
     566    update_postmeta_cache($post_id_list);
     567}
     568
     569function update_postmeta_cache($post_id_list = '') {
     570    global $wpdb, $post_meta_cache;
     571
     572    // We should validate this comma-separated list for the upcoming SQL query
     573    $post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list);
     574
     575    // we're marking each post as having its meta cached (with no keys... empty array), to prevent posts with no meta keys from being queried again
     576    // any posts that DO have keys will have this empty array overwritten with a proper array, down below
     577    $post_id_array = explode(',', $post_id_list);
     578    foreach ( (array) $post_id_array as $pid )
     579        $post_meta_cache[$pid] = array();
     580
    566581    // Get post-meta info
    567582    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) ) {
    568583        // Change from flat structure to hierarchical:
    569         $post_meta_cache = array();
     584        if ( !isset($post_meta_cache) )
     585            $post_meta_cache = array();
     586
    570587        foreach ($meta_list as $metarow) {
    571588            $mpid = (int) $metarow['post_id'];
Note: See TracChangeset for help on using the changeset viewer.