Purge Old Slug Redirects

When the url of a published post is updated in WordPress, the previous url slug is saved into a meta key called _wp_old_slug and a 301 redirect is created automatically. In most cases this is exactly what you would want to happen, but there are rare cases where it can cause a problem.

I hit an odd edge case when upgrading to WordPress 4.4 on WP Engine. At some point the slugs for two WooCommerce products had been swapped, so the _wp_old_slug for ‘product-1’ was ‘product-2’, and ‘product-2’ was ‘product-1’. This should have caused an infinite redirect immediately, but for some reason those rules were ignored until the WordPress 4.4 upgrade.

To resolve the issue, I ran a SQL query to view all the _wp_old_slug redirects:

SELECT * FROM `wp_postmeta` WHERE `meta_key` = '_wp_old_slug'

After determining they could all be removed, I ran this query to delete them:

DELETE FROM `wp_postmeta` WHERE `meta_key` = '_wp_old_slug'

Once the _wp_old_slug keys were removed, the product post loaded perfectly again.

About Devin

I'm a WordPress developer based in Austin, Texas. I run a little theme shop called DevPress and work for a startup called Nano. Find me on twitter @devinsays.

Leave a Reply