此脚本将执行以下操作:
function wpse_287807_replace_url() {
// Get all posts
$query = new WP_Query(array(
\'post_type\' => \'post\',
\'posts_per_page\' => -1,
));
$posts = $query->get_posts();
foreach ($posts as $post) {
// Get permalink
$url = $post->post_name;
// Stop words array
$stop_words = array(
\'-a-\',
\'-the-\',
\'-of-\',
);
// Replacement
$replacement = \'-\';
// Replace url
$new_url = str_replace($stop_words, $replacement, $url);
// Prepare arguments
$args = array(
\'ID\' => $post->ID,
\'post_name\' => $new_url,
);
// Update post
wp_update_post( $args );
}
}
add_action(\'init\', \'wpse_287807_replace_url\');
如果您有许多帖子,请禁用创建
revision
通过添加发布
WP_POST_REVISIONS
常数至
wp-config.php
. 它将加快脚本速度并减少对内存使用的需求。
define( \'WP_POST_REVISIONS\', false );