我不能说我会建议在高负载系统上使用它,如果这些系统应该在HTAccess中使用301\'d或有自己的自定义表来关联的话。下面是一个解决方案,它将获取请求的URL并尝试查找具有匹配元的帖子:
/**
* Redirect 404s based on requested url
*
* @return void
*/
function wpse_226378() {
global $wp;
if( is_404() ) {
$current_url = add_query_arg( $_REQUEST,$wp->request ); // Get current requested URL ( minus the domain )
$page = get_posts( array( // Get a single post that has that URL as a post meta
\'post_type\' => \'page\',
\'posts_page_page\' => 1,
\'meta_key\' => \'old_url\',
\'meta_value\' => sanitize_text_field( $current_url ),
\'fields\' => \'ids\',
) );
if( ! empty( $page ) ) { // IF it\'s found
wp_safe_redirect( get_permalink( $page[0] ), 301 ); // 301 redirect it
exit; // Exit script entirely
}
}
}
add_action( \'template_redirect\', \'wpse_226378\' );