我最后勾搭上了404_template
. 比较30k篇文章有点慢,但301重定向有效。
add_filter( \'404_template\', function( $tpl ) {
$request = filter_input( INPUT_SERVER, \'REQUEST_URI\', FILTER_SANITIZE_STRING );
if ( ! $request ) {
return $tpl;
}
$args = array(
\'posts_per_page\' => 1,
\'orderby\' => \'ID\',
\'order\' => \'DESC\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => \'old_urls\',
\'value\' => $request,
\'compare\' => \'LIKE\',
),
),
);
$posts = get_posts( $args );
$redir = get_permalink($posts[0]->ID);
if ( $redir ) {
wp_redirect( $redir, 301 );
exit;
}
return $tpl;
}, -4000 );