这是一项有问题的任务。
我建议您运行一个一次性任务:
获取所有标记为“business consultants”的帖子运行一个一次性任务,将帖子的一个数组保存在php文件中,当请求在其中一个url上完成时,重定向到新的站点url,删除帖子,我将把所有代码作为插件编写。
I very strongly suggest you to take a full backup of your database before installing this plugin
将以下代码保存在名为
RedirectDeleted.php
, 放入插件文件夹,并激活它。
页面完全加载之前需要同样的秒数,但之后,如果您进入帖子列表页面,您应该会看到所有标记为“business consultants”的帖子从WordPress中删除,此外,在上载文件夹中,您应该会看到一个名为redirect_deleted_business-consultants.php
.
如果有,只需访问如下urlhttp://www.example.com/a-post-slug
(其中“a-post-slug”是贴有ost标签的“业务顾问”的真正slug),您应该被重定向到http://www.example.com/business-consultants/a-post-slug
.
请注意,如果您删除redirect_deleted_business-consultants.php
文件或卸载插件redirect will not work anymore.将插件安装为must-use plugin 可以是一个防止不停车的想法。
伟大的事情是,你可以移动到自己的WP安装任何其他标签!
E、 g.如果您有一个带有slug“business stuff”的标签,并且您想将其移动到另一个WP安装,只需在url上创建此安装http://www.example.com/business-stuff
并在$target_tags
阵列(见下文)。
代码:
<?php
/**
* Plugin Name: RedirectDeletedTag
* Plugin URI: https://wordpress.stackexchange.com/questions/136018/
*/
class RedirectDeletedTag {
/**
* Adjust this variable according to real tag slug.
* The key is the tag slug, and the value is the base url for the redirect.
* If you want, you can add other tag slugs you want ot move to separate sites.
*/
protected $target_tags = array(
\'business-consultants\' => \'http://www.example.com/business-consultants/\',
);
function cacheUrls() {
if ( ! is_admin() || ! current_user_can( \'install_plugins\' ) ) return;
$t = new ArrayIterator( $this->target_tags );
while( $t->valid() ) {
$tag = $t->key();
if ( $this->shouldCache( $tag ) ) $this->cacheTagUrls( $tag );
$t->next();
}
}
function redirect() {
if ( is_admin() ) return;
$url = $this->getUrl();
if ( ! empty( $url ) ) {
$t = new ArrayIterator( $this->target_tags );
while( $t->valid() ) {
$this->redirectTag( $t->key(), $url, $t->current() );
$t->next();
}
}
}
protected function cacheTagUrls ( $tag ) {
$args = array( \'post_type\' => \'post\', \'nopaging\' => true, \'tag\' => $tag );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$slugs = wp_list_pluck( $query->posts, \'post_name\' );
$id_list = implode( \',\', wp_list_pluck( $query->posts, \'ID\') );
$cache = \'<?php return array("\' . implode(\'","\', $slugs ) . \'");\';
if ( @file_put_contents( $this->getCachePath( $tag ), $cache ) !== FALSE ) {
set_transient( __CLASS__ . $tag, \'1\' );
// cache saved, delete all posts and term
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->posts WHERE ID IN ($id_list)" );
$term = get_term_by(\'slug\', $tag, \'post_tag\');
if ( ! empty( $term ) && isset( $term->term_id ) ) {
wp_delete_term( $term->term_id, \'post_tag\' );
}
}
}
}
protected function redirectTag( $tag, $url, $base ) {
if ( file_exists( $this->getCachePath( $tag ) ) ) {
$cached = (array) include $this->getCachePath( $tag );
if ( in_array( $url, $cached ) ) {
wp_redirect( trailingslashit( $base ) . $url );
exit();
}
}
}
protected function shouldCache( $tag = \'\' ) {
$should = FALSE;
if ( is_admin() && current_user_can( \'install_plugins\' ) ) {
$transient = get_transient( __CLASS__ . $tag );
$file = file_exists( $this->getCachePath( $tag ) );
$should = empty($transient) && ! $file;
}
return $should;
}
protected function getCachePath( $tag = \'\' ) {
$upload_dir = wp_upload_dir();
$base = trailingslashit( $upload_dir[\'basedir\'] );
return $base . \'redirect_deleted_\' . $tag . \'.php\';
}
protected function getUrl() {
$home_path = trim( parse_url( home_url(), PHP_URL_PATH ), \'/\' );
$full = trim( str_replace( $home_path, \'\', add_query_arg( array() ) ), \'/\' );
$stripped = explode( \'?\', $full );
return trim( $stripped[0], \'/\' );
}
}
$rd = new RedirectDeletedTag();
add_action( \'shutdown\', array( $rd, \'cacheUrls\' ) );
add_action( \'after_setup_theme\', array( $rd, \'redirect\' ) );
该插件可用作Gist
here.
要运行一个一次性任务,我将使用瞬态加上检查当前用户是否是管理员,再加上缓存文件的存在。如果你愿意,看看这个question/answers 了解WordPress中一次性任务的不同方法。