0

During a website move the wp-config.php file was replaced (by the host) to connect to the new database, and a directive I had in my old version (that specified a root folder for all images instead of the WP default 'uploads' folder) did not make it into the new wp-config.php file.

By the time I noticed it, we had a lot of content added with the images now in the /uploads folder.

I copied all the images now in /wp-content/uploads to /images, but I need a SQL query to cull thru all of the content and find&replace any image URLs that point to the /wp-content/uploads folder.

I am getting more proficient with PHP every day but still very weak with SQL, hoping someone can help me - I don't mind adding a temporary plugin to do search & replace, but it seems like it should be simple to accomplish without a plugin. I do have access to phpMyAdmin and am always super-careful when I use it, backing up my database always before making changes.

2
  • 2
    If you have command-line access (eg via SSH), I highly recommend using wp search-replace {old-url} {new-url} (always always always use --dry-run first to make sure you're not wrecking things).
    – Pat J
    Sep 12, 2022 at 18:29
  • Thank you @PatJ - that sounds simple but I’m unfamiliar with using the command line, much more comfortable with phpMyAdmin....but I'll hang onto this suggestion just in case, thanks!
    – Trisha
    Sep 12, 2022 at 22:50

1 Answer 1

0

If you don't have WPCLI installed, but still have commandline access or access via PHPMyAdmin, run:

UPDATE wp_posts SET post_content  = REPLACE(post_content,'<old URL>','<new URL>');
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value,'<old URL>','<new URL>');
2
  • 1
    I'd be cautious about this; if the old URL is in serialized data (eg an array saved in the postmeta table), and the new URL isn't the same length as the old one, this can break things. Be sure to back up your database first (and know how to restore it, just in case).
    – Pat J
    Sep 12, 2022 at 19:49
  • 1
    Thank you again @PatJ, I think this is the route I'll take but I will be very cautious and check first to see if the URL is serialized, but I don't think so, this is just for those image references contained within the text of the content where authors have added media, the links to the featured image for each Post is easier for me to deal with, I just needed an easier way to catch those in the content without having to go thru thousands of Posts.....
    – Trisha
    Sep 12, 2022 at 22:52

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.