Mass replace in WordPress posts via MySQL query

I was doing a lot of changes to my old posts, switched to HTTPS, etc. Sometimes it was useful to change some particular text in all my old posts at a time, but there is no such feature in WordPress. But WordPress runs on MySQL and I could use SQL query to update such posts.

Make backup – it's not required but strongly advised ๐Ÿ™‚

Now use this query as template to replace in place whatever you need:

mysql> UPDATE wp_posts SET post_content = REPLACE(post_content, '<!--more-->', '');
Query OK, 157 rows affected (0.04 sec)
Rows matched: 455  Changed: 157  Warnings: 0

This will remove <!--more-->from all posts (it's used by WordPress to span article when showed on tag/category pages).

Another example to update all URLs to HTTPS:

UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://gagor.pl', 'https://gagor.pl');

Be careful with that and make DB backup before you start.

2 thoughts on “Mass replace in WordPress posts via MySQL query”

    1. I felt so confident that… I didn’t made backup before ๐Ÿ™‚
      So it was two commands for me ๐Ÿ™‚
      P.S.
      I have daily backups – that’s enough.

Leave a Reply

Your email address will not be published. Required fields are marked *