我对这个问题很感兴趣,所以我试了一下。未测试,因此建议备份wp_posts
运行前的表(尽管您无论如何都应该这样做!)
如评论中所述,don\'t forget to replace "domain.com" in the regular expression with your own.
require \'wp-load.php\'; // Only needed if *not* a plugin.
function out_with_the_old( $match ) {
global $links;
if ( ! isset( $links[ $match[1] ] ) ) { // Cache to save subsequent hits.
$links[ $match[1] ] = get_permalink( $match[1] );
clean_post_cache( $match[1] );
}
return $links[ $match[1] ] ? $links[ $match[1] ] : $match[0]; // Just fallback to original URL if new permalink void (for whatever reason).
}
$links = array();
$posts = get_posts(
array(
\'posts_per_page\' => -1,
\'post_status\' => \'any\',
\'post_type\' => \'any\',
\'fields\' => \'ids\', // Getting *everything* will most likely hit the memory limit.
)
);
// Loop over each post & replace all old instances of the permalink with the new one.
foreach ( $posts as $post_id ) {
$save = array();
$post = get_post( $post_id );
foreach ( array( \'post_content\', \'post_excerpt\' ) as $field ) {
// Replace "domain\\.com" with your domain name, minus "www." (escape dots with a backslash).
// The regex accommodates for various forms of the same URL, including SSL, no "www" & no index.php.
$text = preg_replace_callback( "!https?://(?:www\\.)?domain\\.com/(?:index\\.php)?\\?(?:p|post_id)=([0-9]+)!", \'out_with_the_old\', $post->$field );
if ( $text !== $post->$field )
$save[ $field ] = $text;
}
if ( $save )
$wpdb->update(
$wpdb->posts,
$save,
array(
\'ID\' => $post_id,
),
\'%s\',
\'%d\'
);
clean_post_cache( $post ); // Otherwise we might soon hit a memory limit.
}
您可以将其转换为插件,但最便宜、最简单的方法是将其转储到脚本中,通过FTP传输到WordPress所在的目录,然后在浏览器中运行。