On a WordPress site http://www.example.com/
(MAIN SITE) there are 540000+ posts.
I want to reduce number of posts to enhance site speed.
I’ve installed new WordPress at http://www.example.com/business-consultants/
(NEWSITE) and here I moved from MAIN SITE 3135 posts tagged ‘Business Consultants’.
Currently, I’ve set 301 redirect on MAIN SITE to redirect posts tagged ‘Business Consultants’ to the new WordPress install, that is
{MAIN_SITE}/{slug} => {NEW_SITE}/business-consultants/{slug}
What I do is checking post tag, and if ‘business-consultants’ is present, redirect request to new the new site using wp_safe_redirect()
.
The problem is that to make redirect works I need the post are actually available in MAIN SITE, what I’m looking for is a solution to make redirect work even if posts tagged ‘Business Consultants’ are deleted from main site.
This is a problematic tasks.
What I suggest you is run a one-time task that:
I’ll write all the code as plugin.
I very strongly suggest you to take a full backup of your database before installing this plugin
Save following code in a file called
RedirectDeleted.php
, put in plugin folder, and activate it.It will take same seconds before the page is fully loaded, but after that, if you go on your post list page, you should see all the post tagged ‘business-consultants’ deleted from WordPress, in addition in your uploads folder you should see a php file named
redirect_deleted_business-consultants.php
.If ever, just visit an url like
http://www.example.com/a-post-slug
(where ‘a-post-slug’ is a real slug of a ost tagged ‘business-consultants’) and you should be redirected tohttp://www.example.com/business-consultants/a-post-slug
.Please note that if you ever delete
redirect_deleted_business-consultants.php
file or you unistall the plugin, the redirect will not work anymore.Install the plugin as a must-use plugin can be an idea to prevent unistall.
The great thing is that you can move to its own WP install any other tag!
E.g. if you have a tag with slug ‘business-stuff’ and you want to move it to another WP install, just create this install at the url
http://www.example.com/business-stuff
and add ‘business-stuff’ to the plugin file in the$target_tags
array (see below).The code:
The plugin is available as Gist here.
* To run a one-time task I’ll use a transient plus a check if current user is an administrator, plus the existence of the cache file. If you want, take a look at this question/answers to know different approach to one-time tasks in WordPress.