Mass editing a string in a WordPress MySQL Database

I’ve encountered many times with the need to change a certain string from a database in order to adapt all images and hardcoded links from one url to a different one. But there are more cases you would want to search & change a string by accessing directly your databases:

  1. images not working and you need to change their URL,
  2. changed the home URL in “settings” and can’t access your blog anymore,
  3. changing a phrase or spelling you’ve been using incorrectly all throughout,
  4. changing all instances of the name and slug of an old custom post type to a new one,

Log into your PhpMyadmin panel and search for the SQL console and use this line:

UPDATE wp_posts SET `post_content` = REPLACE (  `post_content`,  'Item to replace here',  'Replacement text here');

Where wp_posts responds to the name of your posts table which will vary depending on your table index.


Or log in via a shell command:

mysql -u username -ppassword
use mywordpress_table;
UPDATE wp_posts SET post_content = REPLACE (  post_content,  'text to find here',  'text to replace here');

Author: Bea Cabrera

Freelance Filmmaker with a passion for big cities, snowboard, cinema and a weakness for the smell of freshly ground coffee. Engineer & Graphic Designer in a previous life, loving and living both: art and technology.  

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">html</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.