1

I want to replace all URLs that start with https://oneweburl.com/cm with https://anotherwebsite.com

i.e. URLs such as

https://oneweburl.com/cm/9304/434
https://oneweburl.com/cm/849/495/34
https://oneweburl.com/cm/2994/234/54

will be replaced with only https://anotherwebsite.com. So far I have tried

update wp_posts 
set post_content = 
    replace(post_content, 'https://oneweburl.com/cm/%', 'https://anotherwebsite.com');

Apparently that didn't work. Any idea the SQL to accomplish this? Thanks!

2
  • 1
    Tag your question with the database you are using. Commented Feb 5, 2018 at 21:56
  • It is MySQL database Commented Feb 5, 2018 at 22:21

1 Answer 1

2

For the most part, you should be able to do:

update wp_posts
    set post_content = replace(post_content, 'https://oneweburl.com/cm/', 'https://anotherwebsite.com/')
    where post_content like 'https://oneweburl.com/cm/%';

This should work in any database.

The one issue is if post_content could have multiple URLs in the string. This will replace all matches to the specified string. If that is an issue, you can do string manipulations to do the right thing. Alas, those tend to depend on the specific database.

Sign up to request clarification or add additional context in comments.

1 Comment

"http%oneweburl.com/cm/%" will handle the differences (https, http, www).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.