I'm newbie in SQL questions, and today i have a big problem.

I need change many fields in my SQL, about 4,5k. But have some rules to the query change, to don't change wrong fields where don't need changed.

In table wp_postmeta i have four columns: meta_id | post_id | meta_key | meta_value

I need change the value of meta_value, BUT ONLY if the current value is 'Y' or 'Empty' and REPLACE to value 'X', AND ONLY in lines where meta_key value is old_price

How i can do it?

1 Answer 1


Recommend a database backup before you do this.

** updated the statement to specify "Y", empty string, or null **

global $wpdb;
$wpdb->query( "update {$wpdb->postmeta} SET meta_value = 'X' WHERE meta_key = 'old_price' AND ( meta_value = 'Y' OR meta_value = '' OR meta_value IS NULL ) );" );
  • Thanks for help! I use your code like base, and after one "google" i make my code and works great. Thank you for the hep. Jan 3, 2020 at 0:32
  • The final code is it: UPDATE wp_postmeta SET meta_value = 'X' WHERE meta_key = 'old_price' AND meta_value LIKE 'Y' OR '' Jan 3, 2020 at 0:34
  • Oh I see, empty like "", not "Empty". Should work fine. You probably could have also done, meta_value = "Y" OR meta_value = "" or meta_value IS NULL
    – Joel M
    Jan 3, 2020 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.