Search and Replace a Custom Field in WordPress using PHPMyAdmin
If you’ve ever used Microsoft Excel, you probably have an idea of just how easy changing hundreds of cells can be. Gone are the days of manually typing line by line any corrections that you need to make. The same is true when you are working with a database like we are in WordPress.
This example performs a search and replace of all custom fields that meet a specified criteria. In this case, I’m looking for all staff member pages that have a custom field for a department phone number that was incorrect. We want to replace that with a new number. Using PHPMyAdmin and running a SQL query, I can select all of them to see the results before I do anything to my databse:
SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'DeptPhone' AND `meta_value` LIKE '212-555-1212'
It is worth mentioning, that you can also use wildcards if you want to include more results. The ‘%’ is your wildcard:
SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '%eptPhone' AND `meta_value` LIKE '212-555-%'
This yields the same results. It is also worth pointing out that the quotation mark around our text values and the tick mark around our table column names are different.
`meta_key`uses tick marks (this is usally the top left key on your keyboard, next to the 1)'%DeptPhone'uses a single quotation mark (this is next to the enter key)
So, to perform the changes, we need to use the UPDATE command instead of select, and actually replace text. Here goes:
UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, '212-555-1212', '212-444-1212') WHERE `meta_key` LIKE 'DeptPhone'
The search and replace plugin (http://bueltge.de/wp-suchen-und-ersetzen-de-plugin/114/) will allow you to do this as well won’t it? Generally I try to do everything through SQL and PHP but given a choice between mucking about with mySQL or a simple plugin, I find this one pretty helpful.
Pingback: Wordpress Find and Replace Plugins | Wordpress Directory
Thank you very much for this. I was hoping the search and replace plugin would let med edit the “post image” field, but I couldn’t figure out how.
Your guide saved the day
Pingback: Find, Search, Replace, and Delete In A WordPress Database - WordCast
Hello there,
I have a question for the webmaster/admin here at brockangelo.com.
May I use part of the information from your blog post right above if I give a link back to your website?
Thanks,
James
Yes, that is fine if you use a trackback.
It works Great! Thanks