在Wordpress中使用两个数据库时,某些功能无法正常工作

时间:2013-07-09 03:14:29

标签: wordpress migration

我正在努力将一个WP站点迁移到一个新站点。两者都相当先进,有许多自定义字段,分类法和帖子类型,所以我不能使用内置插件进行导入/导出。

相反,我在同一台服务器上设置了两个数据库,并可以使用$wpdb->select('old_db_name')$wpdb->select(DB_NAME)在它们之间轻松切换。我为此创建了一个小插件,以便它从WP内部运行,因此允许我使用所有WP方法来获取和插入帖子等。

除了一件事,一切都很好;分类。我尝试过的与分类法相关的所有函数都将查询DB_NAME数据库(即运行WP安装的数据库)。

这是一个简单的例子:

<?php
global $wpdb;

# Use default DB
$wpdb->select(DB_NAME);

# Prints "2" (I've only added two posts to the new installation)
echo count(get_posts(array('numberposts' => -1)));
# Prints all the taxonomies
var_dump(get_taxonomies());

# Switch to old DB
$wpdb->select('old_db_name');

# Prints > "300" (there are roughly 300 posts in the old DB - the number is correct and NOT the same as before)
echo count(get_posts(array('numberposts' => -1)));
# Prints EXACTLY the same thing as the previous call, even though the old DB has different custom taxonomies
var_dump(get_taxonomies());

wp_get_post_terms()也不起作用,而是查询新的数据库(如果我尝试从旧的分类中获取术语,新的数据库中不再存在的WP会引发错误,说分类法不存在)。

这是一个错误吗?有什么方法可以解决吗?关于在WP中使用多个DB的信息不多,所以无法在线找到任何内容。

1 个答案:

答案 0 :(得分:0)

好的,经过几个小时的调查后,似乎WP会在初始化时缓存所有可用的分类法,因此在切换DB时,重新检查可用的分类法。然而 从正确的数据库中选择数据,这不是问题。

我提出的解决方案只是将缺少的分类法添加到新数据库中(暂时),迁移后我将删除它们。