I have a Wordpress site with a couple hundred posts. The posts were being created with batch processing and a piece of code was being inserted dynamically. I found an error in the URL of one of the hyperlinks being inserted into the post content. Every URL has a space instead of the desired "-" character. I'm trying not to manually fix each post.

As you can see, part of the URL below has a space in it and is causing 404 errors in all of the different posts. SQL sees it like this:

<a href="http://www.mysite.com/this-URL-is-broken-because-of-a space"></a>
<a href="http://www.mysite.com/this-URL-is-broken-because-of-a space-also"></a>
<a href="http://www.mysite.com/this-URL-is-broken-because-of-a stupid-space"></a>

Google sees them like this:

<a href="http://www.mysite.com/this-URL-is-broken-because-of-a%20space"></a>
<a href="http://www.mysite.com/this-URL-is-broken-because-of-a%20space-also"></a>
<a href="http://www.mysite.com/this-URL-is-broken-because-of-a%20stupid-space"></a>

Unfortunately, as you can see, the URLs aren't the same, so I can't do a find an replace. I'm hoping someone has some savvy SQL operation to find the space and replace it with the appropriate "-" character. I'm thinking that I could say something like this:

Replace any blank space in the field with the "-" character, unless the blank space belongs to the "a href" string.

SQL Table -> wp_posts Field -> post_content

Maybe there's a .htaccess solution?

Thanks in advance for the help!

1 Answer 1


I think you should run this query. To replace space with dash.

update wp_posts set wp_posts.post_name = REPLACE( wp_posts.post_name, ' ', '-' );

Don't forget to change table_prefix in this. And also, keep a backup on current database before trying.

  • Thanks, but the content is within a hyperlink tag in the POST CONTENT, not in the title. As I mentioned, the field is wp_posts.post_content. Also, this query would replace all spaces in all post titles with dash, which would cause a nightmare for front-end display.
    – unifiedac
    Commented Sep 10, 2014 at 20:43
  • Yes, that will not work for post content. Sorry, I misunderstood.
    – Robert hue
    Commented Sep 10, 2014 at 20:47

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.