出于安全考虑,我会在php中分两部分进行,而不是一个大的原始sql语句,例如在“functions.php”中:
function wpse160706() {
$old_post_type = \'films\';
$new_post_type = \'post\';
$category_slug = \'directors\';
$taxonomy = \'category\';
global $wpdb;
$sql = $wpdb->prepare( \'SELECT p.ID FROM \' . $wpdb->posts . \' AS p\'
. \' JOIN \' . $wpdb->term_relationships . \' AS tr ON p.ID = tr.object_id\'
. \' JOIN \' . $wpdb->term_taxonomy . \' AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id\'
. \' JOIN \' . $wpdb->terms . \' AS t ON tt.term_id = t.term_id\'
. \' WHERE p.post_type = %s AND t.slug = %s AND tt.taxonomy = %s\'
, $old_post_type, $category_slug, $taxonomy );
$post_ids = $wpdb->get_col( $sql );
print("post_ids=" . implode( \',\', $post_ids));
foreach ( $post_ids as $post_id ) {
$sql = $wpdb->prepare( \'UPDATE \' . $wpdb->posts
. \' SET post_type = %s WHERE ID = %d AND post_type = %s\'
, $new_post_type, $post_id, $old_post_type );
// Uncomment the following if happy that $post_ids correct.
//$wpdb->query( $sql );
}
}
然后调用它,确保您只获得了所需的帖子,然后备份数据库等并取消对更新查询的注释,运行一次,然后删除
wpse160706()
呼叫