Skip to content

Commit

Permalink
Post meta caching fix by mdawaffe. fixes #3229
Browse files Browse the repository at this point in the history
git-svn-id: https://develop.svn.wordpress.org/branches/2.0@4373 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
markjaquith committed Oct 11, 2006
1 parent 438130b commit a227557
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
52 changes: 26 additions & 26 deletions wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,10 @@ function delete_option($name) {
function add_post_meta($post_id, $key, $value, $unique = false) {
global $wpdb, $post_meta_cache;

$post_id = (int) $post_id;

if ( $unique ) {
if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
= '$key' AND post_id = '$post_id'") ) {
if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
return false;
}
}
Expand All @@ -434,46 +435,46 @@ function add_post_meta($post_id, $key, $value, $unique = false) {

$wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");

$post_meta_cache['$post_id'][$key][] = $original;
$post_meta_cache[$post_id][$key][] = $original;

return true;
}

function delete_post_meta($post_id, $key, $value = '') {
global $wpdb, $post_meta_cache;

$post_id = (int) $post_id;

if ( empty($value) ) {
$meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
post_id = '$post_id' AND meta_key = '$key'");
$meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
} else {
$meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
$meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
}

if ( !$meta_id )
return false;

if ( empty($value) ) {
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
AND meta_key = '$key'");
unset($post_meta_cache['$post_id'][$key]);
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'");
unset($post_meta_cache[$post_id][$key]);
} else {
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
AND meta_key = '$key' AND meta_value = '$value'");
$cache_key = $post_meta_cache['$post_id'][$key];
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
$cache_key = $post_meta_cache[$post_id][$key];
if ($cache_key) foreach ( $cache_key as $index => $data )
if ( $data == $value )
unset($post_meta_cache['$post_id'][$key][$index]);
unset($post_meta_cache[$post_id][$key][$index]);
}

unset($post_meta_cache['$post_id'][$key]);
unset($post_meta_cache[$post_id][$key]);

return true;
}

function get_post_meta($post_id, $key, $single = false) {
global $wpdb, $post_meta_cache;

$post_id = (int) $post_id;

if ( isset($post_meta_cache[$post_id][$key]) ) {
if ( $single ) {
return maybe_unserialize( $post_meta_cache[$post_id][$key][0] );
Expand Down Expand Up @@ -507,6 +508,8 @@ function get_post_meta($post_id, $key, $single = false) {
function update_post_meta($post_id, $key, $value, $prev_value = '') {
global $wpdb, $post_meta_cache;

$post_id = (int) $post_id;

$original_value = $value;
if ( is_array($value) || is_object($value) )
$value = $wpdb->escape(serialize($value));
Expand All @@ -515,26 +518,23 @@ function update_post_meta($post_id, $key, $value, $prev_value = '') {
if ( is_array($prev_value) || is_object($prev_value) )
$prev_value = $wpdb->escape(serialize($prev_value));

if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
= '$key' AND post_id = '$post_id'") ) {
if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = '$key' AND post_id = '$post_id'") ) {
return false;
}

if ( empty($prev_value) ) {
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
meta_key = '$key' AND post_id = '$post_id'");
$cache_key = $post_meta_cache['$post_id'][$key];
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id'");
$cache_key = $post_meta_cache[$post_id][$key];
if ( !empty($cache_key) )
foreach ($cache_key as $index => $data)
$post_meta_cache['$post_id'][$key][$index] = $original_value;
$post_meta_cache[$post_id][$key][$index] = $original_value;
} else {
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
$cache_key = $post_meta_cache['$post_id'][$key];
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
$cache_key = $post_meta_cache[$post_id][$key];
if ( !empty($cache_key) )
foreach ($cache_key as $index => $data)
if ( $data == $original_prev )
$post_meta_cache['$post_id'][$key][$index] = $original_value;
$post_meta_cache[$post_id][$key][$index] = $original_value;
}

return true;
Expand Down Expand Up @@ -1444,7 +1444,7 @@ function update_post_caches(&$posts) {
// Change from flat structure to hierarchical:
$post_meta_cache = array();
foreach ($meta_list as $metarow) {
$mpid = $metarow['post_id'];
$mpid = (int) $metarow['post_id'];
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];

Expand Down
6 changes: 4 additions & 2 deletions wp-includes/template-functions-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,16 @@ function get_post_custom( $post_id = 0 ) {
if ( ! $post_id )
$post_id = $id;

$post_id = (int) $post_id;

if ( isset($post_meta_cache[$post_id]) )
return $post_meta_cache[$post_id];

if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' ORDER BY post_id, meta_key", ARRAY_A) ) {
// Change from flat structure to hierarchical:
$post_meta_cache = array();
foreach ( $meta_list as $metarow ) {
$mpid = $metarow['post_id'];
$mpid = (int) $metarow['post_id'];
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];

Expand Down Expand Up @@ -256,7 +258,7 @@ function post_custom( $key = '' ) {

// this will probably change at some point...
function the_meta() {
global $id, $post_meta_cache;
global $id;

if ( $keys = get_post_custom_keys() ) {
echo "<ul class='post-meta'>\n";
Expand Down

0 comments on commit a227557

Please sign in to comment.