我正在将大约500篇帖子(实际上是WooCommerce产品)迁移到一个新的自定义帖子类型(使用Convert Post Types插件),并希望同时迁移类别。
有一个Taxonomy Converter插件,它可以迁移类别本身,但不能迁移它们的层次结构,尽管它说会迁移。
还有简单的跑步:
UPDATE wp_term_taxonomy SET taxonomy=\'project-category\' WHERE taxonomy=\'product_cat\';
我可以手动调整层次结构,这是我目前倾向的,但我遇到了
some code 这是几年前的作品,我对它进行了一些更新,希望能看到一些作品。它包括在下面。
递归迭代器返回的每一行如下所示:
Array
(
[term_id] => 39
[description] => Bla bla bla
[parent] => 0
[count] => 7
[name] => Security Bars
[slug] => security-bars
)
但我不清楚这些信息要插入哪里。我认为大致如下:
$qry = $wpdb->insert(\'wp_term_taxonomy\', array(
\'term_id\' => $row[\'term_id\'],
\'taxonomy\' => \'project-category\',
\'description\' => $row[\'description\'],
\'parent\' => $parent_id
));
但这仍然没有给我等级制度。我想这是因为
parent
需要是身份证
wp_term_taxonomy
我刚刚创建了。
有什么建议吗?一个可以做到这一点的插件将受到欢迎。
迄今为止的代码:
class RecursiveCategoryIterator implements RecursiveIterator {
const ID_FIELD = \'term_id\';
const PARENT_FIELD = \'parent\';
private $_data;
private $_root;
private $_position = 0;
public function __construct(array $data, $root_id = 0) {
$this->_data = $data;
$this->_root = $root_id;
}
public function valid() {
return isset($this->_data[$this->_root][$this->_position]);
}
public function hasChildren() {
$subid = $this->_data[$this->_root][$this->_position][self::ID_FIELD];
return isset($this->_data[$subid])
&& is_array($this->_data[$subid]);
}
public function next() {
$this->_position++;
}
public function current() {
return $this->_data[$this->_root][$this->_position];
}
public function getChildren() {
return new self($this->_data,
$this->_data[$this->_root][$this->_position][self::ID_FIELD]);
}
public function rewind() {
$this->_position = 0;
}
public function key() {
return $this->_position;
}
public static function createFromResult($result) {
$menu_array = array();
foreach($result as $row) {
$menu_array[$row[self::PARENT_FIELD]][] = $row;
}
return new self($menu_array);
}
}
使用它的功能:
function migrate_terms() {
$sql="SELECT a.term_id,a.description,a.parent,a.count,b.name,b.slug
FROM wp_term_taxonomy a INNER JOIN wp_terms b WHERE a.term_id=b.term_id
AND a.taxonomy=\'product_cat\';
";
global $wpdb;
$result = $wpdb->get_results($sql, ARRAY_A);
// always test for failure
if($result === false) {
die("query failed: ". $wpdb->show_errors() );
}
// create the iterator from the result set
$wpterms = RecursiveCategoryIterator::createFromResult($result);
// Look at it
echo "<pre>";
print_r($wpterms);
echo "</pre>";
// and import it.
//insert_it($wpterms, 0);
}
以及“完成所有脏活的功能”:
function insert_it($iterator, $parent_id = 0) {
foreach($iterator as $row) {
// insert the row, just edit the query, and don\'t forget
// to escape the values. if you have an insert function,
// use it by all means
$qry = $wpdb->insert(WHAT GOES HERE?);
$status = $wpdb->get_results($qry);
if($status === false) {
// insert failed - rollback and abort
die("hard: " . $wpdb->show_errors() );
}
// you need to pass the id of the new row
// so the "child rows" have their respective parent
$cid = $wpdb->insert_id;
// insert the children too
if($iterator->hasChildren()) {
insert_it($iterator->getChildren(), $cid);
}
}
}
// Now we set that function up to execute when the admin_notices action is called
add_action( \'admin_notices\', \'migrate_terms\' );