Ask your WordPress questions! Pay money and get answers fast! (more info)

change result of update_post_meta in database WordPress

  • REFUNDED

have some issue in wordpress post_meta array its send wrong details in database so i want to change result of update_post_meta in database now i use below post meta:

update_post_meta($post_id, '_wpsc_vendors', array(19,'enabled',true,rate,30 ));
which generate this post-meta key and value

key: _wpsc_vendors
value: a:5:{i:0;i:19;i:1;s:7:"enabled";i:2;b:1;i:3;s:4:"rate";i:4;i:30;}

now i want to change result value too this in database

a:1:{i:19;a:2:{s:7:"enabled";s:4:"true";s:4:"rate";s:2:"30";}}

want to update_post_meta again with different parameters so result will be a:1:{i:19;a:2:{s:7:"enabled";s:4:"true";s:4:"rate";s:2:"30";}}

http://stackoverflow.com/questions/17407186/change-result-of-update-post-meta-in-database/17410174?noredirect=1#17410174

here exactly what i am doing is i installed this two plugin

wordpress.org/plugins/wp-e-commerce/‎
http://www.wpecommercehelp.com/download/1/many-vendors-wpsc-plugin

now when you install both plugin and go to add new product you see there is new metabox for vendor

now i am try to create a frontend plugin which automatically get user id and check the vendor and enter the rate with update post meta.

Answers (1)

2013-07-01

Dbranes answers:

You could try pure SQL to replace the database value:


$meta_key = '_wpsc_vendors';
$meta_value = 'a:1:{i:19;a:2:{s:7:"enabled";s:4:"true";s:4:"rate";s:2:"30";}}';

global $wpdb;
$sql = "UPDATE " . $wpdb->postmeta . " SET meta_value = '" . $meta_value . "' WHERE meta_key = '" . $meta_key. "' AND post_id = ". get_the_ID();
$wpdb->query( $sql );


since you want a semi-serialized string.

<strong>update:</strong> I removed the prepare part, since it might give you problem.


somendra meena comments:

i replace this update_post_meta($post_id, '_wpsc_vendors', array(19,'enabled',true,rate,30 )); with your above code but nothing insert to postmeta


Dbranes comments:

Where do you try this?

When I test this code in <em>single.php</em>:

global $wpdb;

// Udpate
update_post_meta( get_the_ID() , '_wpsc_vendors', array(19,'enabled',true,rate,30 ));

// Before:
$sql = "SELECT meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = '" . $meta_key. "' AND post_id = ". get_the_ID();
$var = $wpdb->get_var( $sql );
printf( "Before: <pre>%s</pre> <hr>", $var );

// Change:
$meta_key = '_wpsc_vendors';
$meta_value = 'a:1:{i:19;a:2:{s:7:"enabled";s:4:"true";s:4:"rate";s:2:"30";}}';
$sql = "UPDATE " . $wpdb->postmeta . " SET meta_value = '" . $meta_value . "' WHERE meta_key = '" . $meta_key. "' AND post_id = ". get_the_ID();
$wpdb->query( $sql );

// After:
$sql = "SELECT meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = '" . $meta_key. "' AND post_id = ". get_the_ID();
$var = $wpdb->get_var( $sql );
printf( "After: <pre>%s</pre> <hr>", $var );


I get:

Before:
a:5:{i:0;i:19;i:1;s:7:"enabled";i:2;b:1;i:3;s:4:"rate";i:4;i:30;}
After:
a:1:{i:19;a:2:{s:7:"enabled";s:4:"true";s:4:"rate";s:2:"30";}}


as expected.


somendra meena comments:

Call to a member function get_var() on a non-object in D:\xampp\htdocs\dwell\wp-content\plugins\wp-store-frontend\lib\support\ecommerce.php on line 35


Dbranes comments:

are you maybe missing this line:

global $wpdb;

in the above code snippet?


somendra meena comments:

i am creating plugin with this

http://wp.tutsplus.com/tutorials/allow-users-to-submit-images-your-site/


somendra meena comments:

i replace my code with your after code but nothing adding there in database ..... i am using this $post_id = wp_insert_post($user_image_data) for insert post as you can see in above url i gave you,,, that why i am using update_post_meta


Dbranes comments:

you should then replace get_the_ID() with $post_id in the above code.

I'm not sure why $wpdb is undefined in your case, is it still giving error?


somendra meena comments:

no its not giving error but not inserting value in database