简体   繁体   中英

Update and Change WordPress URLs in database with REGEXP

I want to change several hundred URLs to the graphics inside the posts. URLs are links to images that are on an inactive imagehost. I had backup and I loading them into a new imagehost, but I have a problem with replacement, because old links have variable, random length.

For example:
hostname.org/image/ ciiidl /file_name.jpeg

Where "ciiidl" is generate random

I wrote a command in SQL, but unfortunately did not replace URLs: UPDATE wp_posts SET post_content = REPLACE (post_content, ' https://old_hostname.org/image/[ ^a-zA-Z0-9]/',' https://new_hostname.com/Img/ ')

I need to use REGEXP, but I do not know how to combine with REPLACE.

I think you just need:

UPDATE wp_posts
    SET post_content = concat(substring_index(post_content, '/image', 1),
                              '/Img/',
                              substring_index(post_content, '/', -1)
                             )
    WHERE post_content LIKE '%/image/%';

Previously I made this changes, but when the link it was static:

UPDATE wp_posts SET post_content= REPLACE(post_content, ' https://old_hostname.org/image/ ',' https://new_hostname.com/img/ ')

I just changed the beginning of the url. The file name has been unchanged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM