URL rewrite based on a custom field value

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.

Read More

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

Related posts

Leave a Reply

2 comments

  1. here is an idea, first add lid to query_vars:

    add_filter('query_vars', 'lid_query_vars');
    function lid_query_vars($vars) {
        // add lid to the valid list of variables
        $new_vars = array('lid');
        $vars = $new_vars + $vars;
        return $vars;
    }
    

    then use parse_request hook to create your redirect

    add_action('parse_request', 'lid_parse_request');
    function lid_parse_request($wp) {
        // only process requests with "lid"
        if (array_key_exists('lid', $wp->query_vars) && $wp->query_vars['lid'] != '') {
            $args = array('meta_key' => 'lid', 'meta_value' => $wp->query_vars['lid']);
            $redirect_to_post = get_posts($args);
            foreach($redirect_to_post as $p){
                $link = get_permalink($p->ID);
                wp_redirect( $link , 301 ); 
                exit;
            }
        }
    }
    
  2. Hi @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 a query_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' Hook

    Lastly 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:

    add_action('parse_request','oldsite_redirect',0);  // 0=before (most) 'parse_request' calls
    function oldsite_redirect() {
      if (isset($_GET['lid'])) {
        global $wpdb;
        $sql = "SELECT post_id FROM {$wpdb->postmeta} " . 
               "WHERE meta_key='lid' AND meta_value='%s'";
        $sql = $wpdb->prepare($sql,$_GET['lid']);
        $post_id = $wpdb->get_var($sql);
        if ($post_id) {
          $permalink = get_permalink($post_id);
          if ($permalink) {
            wp_safe_redirect($permalink,301);
            exit;
          }
        }
      }
    }
    

    Again, either solution will work; @Bainternet’s or mine; your choice which to use.