0

I have a huge blog with over 10K posts and a lot of posts with some adult keywords in titles, urls, content and tags. I am trying to remove all adult ones with "search and replace" plugin. It works fine, but the problem is the url slugs also changes if there is a bad keyword in the url and the original url becomes 404.

For example, http://www.domain.com/adult-keyword-url changes to http://www.renamed-keyword-url which is fine, but the adult keyword url is now 404 and does not redirect to renamed url. Is there a way that the original url automatically do 301 redirects to the changed url?

I tried using the redirection plugin too which claims to auto-redirect such posts. It works fine only when you manually change the url slug of a post and update a post. But it does not work in this case when you are updating a lot of posts by directly changing the database with search and replace plugin.

1 Answer 1

1

If you look in the database in the $wpdb->postmeta table you will see a meta_key named _wp_old_slug. That is how WordPress tracks slug changes. By editing the database directly you bypassed the mechanisms that create that postmeta entry.

You never want to edit the database directly if you can use WordPress Core functions instead. Attempting that kind of shortcut only causes problems, expecially if you don't have a deep and thorough grasp of WordPress internals.

What you want to do instead is run a function similar to this one from another answer:

function fix_post_slugs() {
  $posts = new WP_Query(array('post_type'=>'book'));
  if (empty($posts->posts)) return false;
  foreach ($posts->posts as $p) {
    wp_update_post((array)$p);
  }
}
fix_post_slugs();

In your case, this will do it I believe:

function fix_post_slugs() {
  $posts = new WP_Query(array('post_type'=>'post','post_per_page' =>3));
  if (empty($posts->posts)) return false;
  foreach ($posts->posts as $p) {
    // edit below to search and replace as needed
    $p->post_title = str_replace(' ','+',$p->post_title);
    $p->post_name = sanitize_title_with_dashes($p->post_title);
    wp_update_post((array)$p);
  }
}
fix_post_slugs();

I don't know if you are in a position to back up and do that. If not, you will have to manually map your old slugs to the new ones.

5
  • Thanks for your reply. I pasted the code in my theme's functions.php file. And tried again by replacing a new keyword, but nope it doesn't work. I think your code works when the url slug is blank? In my case its not blank, its renamed. For example, if I want to change the keyword dildo to d___o & I use search and replace plugin, the url domain.com/dildo-url changes to domain.com/d___0-url . The original url goes 404 while the new one works fine. I just want the original one to redirect to new one. I am doing this on a test domain right now with the same database. Jul 27, 2013 at 18:01
  • My code explicitly replaces the slug-- that is the oddly named post_name property. I did test this code. The appropriate entries were created in postmeta, and the old slugs redirect correctly to the new ones. I think something else is going wrong, perhaps a side effect of what you have already tried. Are you trying to redirect CPTs by any chance?
    – s_ha_dum
    Jul 27, 2013 at 18:04
  • Well I had a redirection plugin installed, but disabled it now and tried again but no luck. The old url is not redirecting to the new one. I have not changed anything else to the best of my knowledge. The posts and slugs gets renamed correctly but the old url fails to auto-redirect at new one. :( Jul 27, 2013 at 18:35
  • Ok, I did a mistake- I pasted your code as it is in functions.php & didn't noticed the edits it needs. But, even then if I use it correctly it will only replace 1 keyword at a time and I have a list of 400 adult keywords. And it will only replace titles and url slugs while I want to replace entries in all wp tables like posts, comments, tags, etc. which is only possible by search/replace plugin. I think we need an alternate solution/ code here. I can do everything I need with the plugin except the redirection to changed urls, which is the main problem. Jul 27, 2013 at 18:58
  • Obviously you need a much more complicated replace function but the this is the idea. Look into PHPs preg_replace.
    – s_ha_dum
    Jul 27, 2013 at 19:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.