Add non-existent values instead of returning g false in update_post_meta(). Props filosofo. fixes #7018

git-svn-id: http://svn.automattic.com/wordpress/trunk@7978 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-05-22 00:01:00 +00:00
parent 631665ffe5
commit 3846bff484
2 changed files with 14 additions and 9 deletions

View File

@ -389,21 +389,25 @@ function has_meta( $postid ) {
}
function update_meta( $mid, $mkey, $mvalue ) {
function update_meta( $meta_id, $meta_key, $meta_value ) {
global $wpdb;
$protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
if ( in_array($mkey, $protected) )
if ( in_array($meta_key, $protected) )
return false;
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $meta_id) );
wp_cache_delete($post_id, 'post_meta');
$mvalue = maybe_serialize( stripslashes( $mvalue ));
$mvalue = $wpdb->escape( $mvalue );
$mid = (int) $mid;
return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_key = %s, meta_value = %s WHERE meta_id = %d", $mkey, $mvalue, $mid) );
$meta_value = maybe_serialize( stripslashes( $meta_value ));
$meta_value = $wpdb->escape( $meta_value );
$meta_id = (int) $meta_id;
$data = compact( 'meta_key', 'meta_value' );
$where = compact( 'meta_id' );
return $wpdb->update( $wpdb->postmeta, $data, $where );
}
//

View File

@ -577,8 +577,9 @@ function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
// expected_slashed ($meta_key)
$meta_key = stripslashes($meta_key);
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) )
return false;
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post_id ) ) ) {
return add_post_meta($post_id, $meta_key, $meta_value);
}
$data = compact( 'meta_value' );
$where = compact( 'meta_key', 'post_id' );