Are you importing posts from another CMS? Do you want to avoid an .htaccess file with a million redirects? Of course you do! Step 1: store your old pages’ paths in a custom field during your import. Step 2: adapt this little function to your site. When a user lands on a 404 error, WordPress checks to see if the requested path exists in a custom field. If so, it redirects the user to the correct post. In this case, my posts imported from Drupal had the old paths stored in the ‘drupal_path’ field. Change your ‘meta_key’ to match your custom field name. <?php add_filter( 'redirect_canonical', 'my_404_no_guessing', 10, 2 ); function my_404_no_guessing( $redirect_url, $request_url ) { if ( is_404() ) { // Drupal redirects $posts = get_posts( array( 'meta_key' => 'drupal_path', // change to your meta_key 'meta_value' => parse_url( $request_url, PHP_URL_PATH ), 'post_type' => 'any', 'posts_per_page' => 1, 'fields' => 'ids', ) ); if ( $posts ) { $redirect_url = get_permalink( array_pop( $posts ) ); } else return false; } return $redirect_url; } Try visiting one of the old URLs. You should be whisked to the new location. This function also prevents
Share This