I’m working on a migration from another CMS to WordPress. The old site had terrible SEO-unfriendly URLs in the format http://example.com?lid=1234
.
We have imported all the posts from the old site into WordPress and are storing the lid
as a custom field.
We would love the old URLs to still work if possible, but as there are about 3,000 posts using .htaccess is out of the question.
So my question is: how would I create a rewrite rule that extracts the lid
value from the URL and redirects to the post that contains it in the custom field? (the lid
value is unique, so there are no worries about more than one post with the same custom field value)
Many thanks
Simon
here is an idea, first add
lid
to query_vars:then use
parse_request
hook to create your redirectHi @Simon Blackbourn:
While I was writing my answer but before I could post it @Bainternet jumped in with a great answer too. He took a slightly different approach but his works equally as well as mine. Still, since I’d already written mine when I saw his I’ll submit mine as an alternate for you to consider.
Uses Direct SQL:
$post_id = $wpdb->get_var($sql)
My solution uses direct SQL which is often not the best approach but I feel it is fair here because of this SQL’s simplicity (and is thus unlikely to break in the future) and because for your needs there is no benefit to using the comparatively heavy
get_posts()
with all its need to process hooks since your needs are one-off for your specific site.Uses Direct URL Parameters:
$_GET['lid']
Additionally I chose to use direct access to
$_GET['lid']
instead of first creating aquery_var
because query vars are part of the standard WordPress query architecture that is use by the underlying URL rewrite system, and for your use-case you are explicitly not leveraging the WordPress query architecture. Instead you need to capture an actual query parameter passed on the URL so I think there isn’t really a need for the overhead of adding a query var (although truth be told using a query var doesn’t hurt, and it works fine too.)Uses the
'parse_request'
HookLastly I did like @Bainternet use of
'parse_request'
hook; it runs before WordPress’ default MySQL query, so you don’t have to run a MySQL query you will just throw away yet it runs after the'init'
hook which means (most) hooks that allow plugins to modify permalinks will have been added thus giving you the correct URLs for your redirects.Place in
functions.php
You can place the following code into your theme’s
functions.php
file:Again, either solution will work; @Bainternet’s or mine; your choice which to use.